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 caseI 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 overwrittenBut, 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()
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:
Derived classes come before their base classes.
Multiple base classes come in the same order they're declared.
Each class only occurs once.
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: