0

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.

6
  • 2
    modifier(declaration) should be modifier(instance). You want to pass in the instance, not the class itself. Commented Aug 26, 2022 at 18:46
  • 2
    The python style guide, PEP8: Class Names suggests capitalizing the first letter of class names and using lower case for variable names. This makes it easier to tell them apart. Had you used class Declaration: it would have been easier to spot the error with modifier(declaration). Commented Aug 26, 2022 at 18:52
  • I tried using modifier(instance) earlier but, then, I was getting a similar error when printing out declaration.info1 at controller at the line print ('MIDDLE ...) Commented Aug 26, 2022 at 18:53
  • 2
    declaration is a class and doesn't have a info1 attribute. Only instance of declaration have that attribute. Commented Aug 26, 2022 at 18:55
  • 1
    @afernandezody Right, because declaration.info1 doesn't make sense. The class itself doesn't have that property. Instances of that class do. Commented Aug 26, 2022 at 18:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.