r/learnpython • u/Crul_ Crul • Jun 24 '22
I don't understand how `super(ParentOrCurrentClass, self).__init__()` works ... with multiple inheritance
Ok, I now understand how super(ParentOrCurrentClass, self).__init__() works with single inheritance.
But I don't understand the multiple inheritance case. Take this example (link to runable page, code pasted on bottom).
- I get how
ChildD().fooandChildE().fooworks, asuming that python takes the first parent class by default, which isBaseAin this case - I also understand how
ChildG().fooworks, because [EDIT: this is WRONG, see comments]super(BaseB, self).__init__()means "execute the constructor of the Parent class of BaseB", which is justObject, soself.foo = "foo Child G"is never overwritten - But, how is it possible that
ChildF().fooreturnsfoo Base Bif we're callingsuper(BaseA, self).__init__()andBaseBis not a parent class ofBaseA?
Thanks
Code
class BaseA():
def __init__(self):
self.foo = "foo Base A"
class BaseB():
def __init__(self):
self.foo = "foo Base B"
class ChildD(BaseA, BaseB):
def __init__(self):
self.foo = "foo Child D"
super().__init__()
class ChildE(BaseA, BaseB):
def __init__(self):
self.foo = "foo Child E"
super(ChildE, self).__init__()
class ChildF(BaseA, BaseB):
def __init__(self):
self.foo = "foo Child F"
super(BaseA, self).__init__()
class ChildG(BaseA, BaseB):
def __init__(self):
self.foo = "foo Child G"
super(BaseB, self).__init__()
def test():
print("ChildD:", ChildD().foo) # ChildD: foo Base A
print("ChildE:", ChildE().foo) # ChildE: foo Base A
print("ChildF:", ChildF().foo) # ChildF: foo Base B
print("ChildG:", ChildG().foo) # ChildG: foo Child G
test()
6
Upvotes
2
u/Ihaveamodel3 Jun 24 '22
There are YouTube videos out there on inheritance that explain this much better than I do.
But essentially, the class inheritance is turned into an ordered list. This can get complicated and I don’t know how to explain it so I’ll leave it at that.
In your case, this list is [BaseA, BaseB].
So if you ask Super to start at BaseA, the next parent is BaseB.
Also, if BaseA and BaseB also had calls to super (which they probably should if you are doing complicated things like multiple inheritance), then Child D and Child E would switch to reporting BaseB.
2
u/TangibleLight Jun 24 '22 edited Jun 24 '22
As /u/Ihaveamodel3 mentions, the inheritance hierarchy is converted to an ordered list. The algorithm is called C3 linearization.
In short:
The resulting list is the Method Resolution Order (MRO). You can check it with
cls.__mro__. For example:What
super(cls, self)actually does is this: look attype(self).__mro__, and find the class immediately aftercls. It produces a proxy object that fetches attributes using that class.So hopefully these make sense:
super()without arguments uses some inspection magic to invokesuper(cls, self)with the containing class.So you can see how, if each class'
__init__method invokessuper().__init__(), then the calls walk the MRO and end up invoking all the initializers.ChildD.__init__would usesuper(ChildD, self)to invokeBaseA.__init__. Then that invokesBaseB.__init__, then that invokesobject.__init__.That's why it's always important to call
super().__init__(). If you don't, that call chain is broken and your base classes don't get initialized.Edit:
Oop. Just saw your comment here: https://www.reddit.com/r/learnpython/comments/vjmddf/i_dont_understand_how_superparentorcurrentclass/idjq8tx/
Yes, that's the gist.
Also worth pointing out that it's possible to construct hierarchies where the MRO constraints can't be satisified. For example:
The issue is that since
Bottom's bases are(Top, Middle),Topmust come beforeMiddlein the MRO. ButMiddleis derived fromTop, soTopmust come afterMiddlein the MRO. There's a contradiction, so the type is invalid.You can resolve it by switching the order of bases, so there is no contradiction:
-1
u/m0us3_rat Jun 24 '22 edited Jun 24 '22
magic or Cthulhu or the computer hates you or it's exactly the result you are supposed to get by executing that code.
pick one.
sorry to be a little bit rude.. but its one important thing to learn early..
if the result is "unexpected" the only true conclusion is that it's your fault.
it's never the computer. even when it is .. it really isn't.