How does super() work in the context of multiple inheritance, and why is it more than just 'call the parent class'?
Short answer
super() doesn't literally mean 'my direct parent class' -- it returns a proxy that follows the class's MRO, calling the *next* class in that order relative to the current class.
Advertisement
AModel answer
super() doesn't literally mean 'my direct parent class' -- it returns a proxy that follows the class's MRO, calling the *next* class in that order relative to the current class. In multiple inheritance with cooperative super() calls throughout a hierarchy, this enables each class's method to properly chain to the next one in the linearized order, not just its immediate base, making cooperative multiple inheritance work correctly.
class A:
def greet(self):
print('A.greet')
class B(A):
def greet(self):
print('B.greet')
super().greet() # calls next in MRO, not necessarily A directly
class C(A):
def greet(self):
print('C.greet')
super().greet()
class D(B, C):
def greet(self):
print('D.greet')
super().greet()
D().greet() # D.greet B.greet C.greet A.greet -- follows MRO, calls each onceReal-world example
A mixin-heavy Django class-based view relies on every mixin calling super().dispatch() cooperatively, so all mixins in the MRO chain execute in the correct order for a single request.
Likely follow-ups
- What breaks if a class in the chain forgets to call super()?
- How does super() differ when called with explicit arguments super(B, self)?
Related topics