How to update variables passed between classes?

Question:

I’m trying to pass variables between different classes. In order to accompish this task, I have created an info class (here called ‘declaration’) so that the code reads:

class declaration():
    def __init__(self):
        self.info1 = 999
        self.info2 = 'something_else'
        print ('At declaration ')

class controller():
    def __init__(self):
        instance = declaration()
        print ('Initial number ',instance.info1, instance.info2)
        modifier(declaration)
        print ('MIDDLE ',instance.info1,declaration.info1)
        instance = declaration()
        print ('Final number ',instance.info1)

class modifier():
    def __init__(self,aux):
        print ('MODIFIER')
        self.info=aux
        self.info.info1=55555

controller()

The output is:

At declaration
Initial number  999 
something else
MODIFIER
MIDDLE 999 55555
At declaration
Final number  999

However, I’m not really sure about some of the inners of the code. I have one major question and a minor one. My main question is that when the class ‘modifier’ is modified according to:

class modifier():
    def __init__(self,aux):
        self.info=aux
        print ('MODIFIER',self.info.info1)
        self.info.info1=55555

it produces the error AttributeError: type object 'declaration' has no attribute 'info1' [Flipping the last 2 lines fixes the error]. It’s confusing (at least to me) whether the class attributes are not passed or they have to be reinitialized.
The second question is how to update instance once its class has been updated. The second call to instance = declaration() seems to accomplish nothing.

Asked By: afernandezody

||

Answers:

Quick side note: Yes I do realise, I just want to say please try to follow the PEP8 python guide as it makes your code look cooler (and easier to read) and all the cool kids use it.

There are a few things wrong with your code, calling modifier(declaration) actually makes the aux parameter an uninitilized class, you want to call modifier(instance) as the init function has already been ran.

Also it would be easier to drop the self.info = aux as you can just call aux.info1 and it looks cleaner and is actually faster (Because you are calling one less Fast Store command in bytecode).

Lastly at print ('MIDDLE ',instance.info1,declaration.info1) you again parse declaration uninitilized therefore you get the error AttributeError: type object 'declaration' has no attribute 'info1', to fix this simply put declaration().info1 as that calls the init function (it is the same as saying declaration.__init__().info1).

So finally you get:

class declaration():
    def __init__(self):
        self.info1 = 999
        self.info2 = 'something_else'
        print ('At declaration ')

class controller():
    def __init__(self):
        instance = declaration()
        print ('Initial number ', instance.info1, instance.info2)
        modifier(instance)
        print ('MIDDLE ', instance.info1, declaration().info1)
        instance = declaration()
        print ('Final number ',instance.info1)

class modifier():
    def __init__(self, aux):
        print ('MODIFIER')
        aux.info1 = 55555

controller()

Hope this helped.

Answered By: xihtyM
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.