The main difference is that ChildA.__init__
will unconditionally call Base.__init__
whereas ChildB.__init__
will call __init__
in whatever class happens to be ChildB
ancestor in self
's line of ancestors
(which may differ from what you expect).
If you add a ClassC
that uses multiple inheritance:
class Mixin(Base):
def __init__(self):
print "Mixin stuff"
super(Mixin, self).__init__()
class ChildC(ChildB, Mixin): # Mixin is now between ChildB and Base
pass
ChildC()
help(ChildC) # shows that the Method Resolution Order is ChildC->ChildB->Mixin->Base
then Base
is no longer the parent of ChildB
for ChildC
instances. Now super(ChildB, self)
will point to Mixin
if self
is a ChildC
instance.
You have inserted Mixin
in between ChildB
and Base
. And you can take advantage of it with super()
So if you are designed your classes so that they can be used in a Cooperative Multiple Inheritance scenario, you use super
because you don't really know who is going to be the ancestor at runtime.
The super considered super post and pycon 2015 accompanying video explain this pretty well.