1

I am trying to call an instance variable from a "parent" class (subclass) to it's "child" class (subsubclass)

class mainclass():
    def __init__(self):
        self.mainclassvar1 = "mainclass"

class subclass(mainclass):
    def __init__(self):
        self.subclassvar1 = "subclass"

    def changeval(self):
        self.subclassvar1 = "subclassedited"

class subsubclass(subclass):
    def __init__(self):
        self.subsubclassvar1 =  subclass.subclassvar1 #<- naturally this fails


def handler():
    main=mainclass()
    sub = subclass()
    sub.changeval()
    subsub = subsubclass()
    print(subsub.subsubclassvar1)# <- how do I achieve this? I would expect "subclassedited" but it doesn't
if __name__ == "__main__":
    handler()

The above does not work obviously but I am trying to show what I am trying to achieve in my head.

if I change the class subsubclass(subclass) as follows it semi-works:

class subsubclass(subclass):
    def __init__(self):
        subclass.__init__(self)
        self.subsubclassvar1 =  self.subclassvar1

however the returned value is the original default value of subclass instead of the expected subclassedited.

I am not sure if I should even be trying to do this but I've got some code where the logic has now come to this point and I want to try see if I can get details from the middle class in to the final child class in their final modified states instead of the defaults and without refactoring a lot of code.

3
  • Does this answer your question? Why do attribute references act like this with Python inheritance? Commented Apr 23, 2022 at 21:53
  • I'm not sure. Are you saying that I cannot do what I am trying to do and should rather set class variables in my middle class which I can call from the very last class? Commented Apr 23, 2022 at 22:10
  • Each __init__ should be calling the parent's __init__ method, which will automatically set the other attributes on self. There's really no need to set subsubclassvar1 to the value of the "parent's" subclassvar1 attribute, because self itself should have subclassvar1 already. Commented Apr 23, 2022 at 22:25

1 Answer 1

1

Each __init__ method should be invoking the parent's __init__ method, so that the instance is properly initialized for all the classes in the hierarchy.

class mainclass:
    def __init__(self):
        super().__init__()
        self.mainclassvar1 = "mainclass"

class subclass(mainclass):
    def __init__(self):
        super().__init__()
        self.subclassvar1 = "subclass"

    def changeval(self):
        self.subclassvar1 = "subclassedited"

class subsubclass(subclass):
    def __init__(self):
        super().__init__()
        # Not sure why you would really need this, but...
        self.subsubclassvar1 =  self.subclassvar1

There's no reason, though that subsub.subclassvar1 should be related to sub.subclassvar1, though. Calling sub.changeval() has nothing to do with subsub.

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

4 Comments

okay, this actually moves me closer to what I want. Thanks @chepner. But my middle class subclass has a bunch of logic in it that changes the original instance variables that are set at init. So when I use super and call the required variable from my 3rd class I only get the original variable before they were changed. Am I approaching this wrong?
@LlewellynCrossley you'll have to call the methods of your super class in your class if that change is to be seen.
@LlewellynCrossley The methods change the variables for a particular instance. You seem to be confusing inheritance and instantiation somewhat.
yep, I've confused inheritance and instantiation. What I'm trying to do should be reading the data from a particular instance instead of using inheritance to read those values. my bad.

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.