0

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.

  • Does this answer your question? [Why do attribute references act like this with Python inheritance?](https://stackoverflow.com/questions/206734/why-do-attribute-references-act-like-this-with-python-inheritance) – Alias Cartellano Apr 23 '22 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? – Llewellyn Crossley Apr 23 '22 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. – chepner Apr 23 '22 at 22:25

1 Answers1

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 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? – Llewellyn Crossley Apr 23 '22 at 22:46
  • @LlewellynCrossley you'll have to call the methods of your super class in your class if that change is to be seen. – Alias Cartellano Apr 23 '22 at 22:51
  • 1
    @LlewellynCrossley The methods change the variables for a particular *instance*. You seem to be confusing inheritance and instantiation somewhat. – chepner Apr 23 '22 at 22:54
  • 1
    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. – Llewellyn Crossley Apr 25 '22 at 15:30