Inheritance and MRO
Python · Core Language · Run with python3
Explanation
Python supports multiple inheritance. The Method Resolution Order (MRO) determines which class's method is called when multiple bases define the same name. Python uses the C3 linearization algorithm — `ClassName.__mro__` shows the order. `super()` follows the MRO, making cooperative multiple inheritance possible (all `__init__` methods in the hierarchy can be called in sequence using `super()`).
Real-World Scenario
A Django model (`MyModel(TimeStampedMixin, SoftDeleteMixin, models.Model)`) uses multiple inheritance to layer created/updated timestamps and soft-delete behaviour on top of the ORM base. Each mixin calls `super().__init__()` so all initializers run in MRO order. ```python class A: def who(self): return "A" class B(A): def who(self): return f"B -> {super().who()}" class C(A): def who(self): return f"C -> {super().who()}" class D(B, C): def who(self): return f"D -> {super().who()}" d = D() print(d.who()) # D -> B -> C -> A print(D.__mro__) # (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>) ```
Quick Snippet
class A:
def who(self): return "A"
class B(A):
def who(self): return f"B -> {super().who()}"
class C(A):
def who(self): return f"C -> {super().who()}"
class D(B, C):
def who(self): return f"D -> {super().who()}"
d = D()
print(d.who()) # D -> B -> C -> A
print(D.__mro__) # (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)Full Example
# Compile: python3 inheritance_mro.py
# Output:
# D -> B -> C -> A
# (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
class A:
def who(self): return "A"
class B(A):
def who(self): return f"B -> {super().who()}"
class C(A):
def who(self): return f"C -> {super().who()}"
class D(B, C):
def who(self): return f"D -> {super().who()}"
d = D()
print(d.who())
print(D.__mro__)
# Cooperative __init__ with super()
class TimestampMixin:
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.created_at = "2024-01-01"
class TagMixin:
def __init__(self, tags=None, **kwargs):
super().__init__(**kwargs)
self.tags = tags or []
class Article(TimestampMixin, TagMixin):
def __init__(self, title, **kwargs):
super().__init__(**kwargs)
self.title = title
a = Article("Hello World", tags=["python", "tutorial"])
print(a.title, a.tags, a.created_at)
python3 inheritance_mro.py