DRPK
DRPK

Reputation: 2091

How to update/change one class init arguments by another class?

I have two classes which i want to call them class A and class B; i need to change some init values of class A by class B globally without making/returning any new objects from class A (class B should not read main app variables and should not make global variables from them).

Here is what i did:

class A:
    def __init__(self):
        self.blah1_is_beautiful = True
        self.blah2_is_beautiful = True
        self.blah3_is_beautiful = True


class B:
    def __init__(self):
        self.null_is_null = None

    def change_some_values(self):
        # Class A instance should update here
        A.blah1_is_beautiful = False
        A.blah2_is_beautiful = False
        A.blah3_is_beautiful = False


obj1 = B()
obj1.change_some_values()
# Some codes ...
new_A_class = A()
print(new_A_class.blah1_is_beautiful)

But i could not get my expected result.

Notice: i do not want to make an object from class A on my main app or call it as global variable in class B and change its instance; Class B should update class A instance by itself without using any outer variables.

Any help on this is appreciated.

Upvotes: 0

Views: 11546

Answers (3)

Victor
Victor

Reputation: 13

You can just add an extra parameter when calling the function like so:

class A:
    def __init__(self):
        self.blah1_is_beautiful = True
        self.blah2_is_beautiful = True
        self.blah3_is_beautiful = True


class B:
    def __init__(self):
        self.null_is_null = None

    def change_some_values(self, name):
        name.blah1_is_beautiful = False
        name.blah2_is_beautiful = False
        name.blah3_is_beautiful = False


obj_a = A()
obj_b = B()
obj_b.change_some_values(obj_a)
print(obj_a.blah1_is_beautiful)

since the values of self.blah1_is_beautiful are going to be unique for every instance of the class you might want to specify which instances attributes you want to change.

If you want the values to change for all instances of the A class just use :

class A:
    blah1_is_beautiful = True
    blah2_is_beautiful = True
    blah3_is_beautiful = True
    def __init__(self):
       pass


class B:
    def __init__(self):
        self.null_is_null = None

    def change_some_values(self):
        A.blah1_is_beautiful = False
        A.blah2_is_beautiful = False
        A.blah3_is_beautiful = False


obj_a = A()
obj_b = B()
obj_b.change_some_values()
print(obj_a.blah1_is_beautiful)

Upvotes: 1

Hendrik
Hendrik

Reputation: 821

Right now your __init__ method sets the values of your properties to True every time the new object is initiated. In your change_some_values method you are only changing the static properties of class A, but aren't using these for anything. Somethign like this could help you out.

class A:
    blah1_is_beautiful = True
    blah2_is_beautiful = True
    blah3_is_beautiful = True

    def __init__(self):
        self.blah1_is_beautiful = A.blah1_is_beautiful
        self.blah2_is_beautiful = A.blah2_is_beautiful
        self.blah3_is_beautiful = A.blah3_is_beautiful


class B:
    def __init__(self):
        self.null_is_null = None

    def change_some_values(self):
        A.blah1_is_beautiful = False
        A.blah2_is_beautiful = False
        A.blah3_is_beautiful = False


obj1 = B()
obj1.change_some_values()
obj2 = A()
print(obj2.blah1_is_beautiful)

https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

Upvotes: 2

Guilherme Correa
Guilherme Correa

Reputation: 594

I'm a bit confused, you want to change an instance of the class A, or the class itself? To change obj2 this code worked for me:

class A:
    def __init__(self):
        self.blah1_is_beautiful = True
        self.blah2_is_beautiful = True
        self.blah3_is_beautiful = True


class B:
    def __init__(self):
        self.null_is_null = None

    def change_some_values(self):
        global obj2
        obj2.blah1_is_beautiful = False
        obj2.blah2_is_beautiful = False
        obj2.blah3_is_beautiful = False

obj1 = B()
obj2 = A()
obj1.change_some_values()
print(obj2.blah1_is_beautiful)

Notice the change of order when instantiating the classes, otherwise you'll get an "obj2 is not defined" error.

Upvotes: 1

Related Questions