Method Resolution Order (MRO) in Python
OOP Internals
Method Resolution Order
Python uses the C3 linearization algorithm to pick the next class in an inheritance chain. Master it to debug complex hierarchies.
Introspect
class A: ...
class B(A): ...
class C(A): ...
class D(B, C): ...
print(D.__mro__)
# (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
Use inspect.getmro() or the __mro__ attribute to debug lookups.
Rules of C3 linearization
- A class appears before its parents.
- Order respects the order listed in the class definition.
- Parents maintain their relative order.
Violations raise TypeError: Cannot create a consistent method resolution order.