Method Resolution Order
Method Resolution Order (MRO) is the sequence Python follows to search for a method or attribute across a class and its parent classes - especially important to understand when a class uses multiple inheritance.
Why MRO Matters
When a class inherits from more than one parent, and multiple parents define a method with the same name, Python needs a clear, consistent rule to decide which version actually gets called. That rule is the MRO.
class A:
def greet(self):
print("Hello from A")
class B(A):
def greet(self):
print("Hello from B")
class C(A):
def greet(self):
print("Hello from C")
class D(B, C):
pass
d = D()
d.greet() # Hello from B
Checking the MRO
You can inspect the exact resolution order Python will follow using the mro() method or the __mro__ attribute.
print(D.mro())
# [D, B, C, A, object]
Python uses an algorithm called C3 linearization to compute this order, which ensures a consistent, predictable search path: it checks the class itself first, then its parents left to right, without ever checking a parent before one of its own children.
Coming Up Next
Next, you'll learn about Overloading - how Python handles methods and operators that need to behave differently depending on their input.
Working Out C3 Linearization by Hand
You can predict any MRO yourself with a simple merge rule. For class D(B, C), Python computes:
MRO(D) = D + merge(MRO(B), MRO(C), [B, C])
The parent lists are MRO(B) = [B, A, object] and MRO(C) = [C, A, object]. The merge repeatedly takes the first head that does not appear in the tail (any position after the first) of the other lists:
- Lists: [B, A, object], [C, A, object], [B, C]. B is not in any tail, so take B. Result: D, B.
- Lists: [A, object], [C, A, object], [C]. A appears in the tail of the second list, so it must wait. Try the next head, C. It is not in any tail, so take C. Result: D, B, C.
- Lists: [A, object], [A, object]. Take A, then object.
Final order: D, B, C, A, object, which matches D.mro(). The step where A had to wait is the heart of C3: a shared parent always comes after all the classes that inherit from it.
super() Follows the MRO, Not the Parent
A common belief is that super() calls "the parent class". In fact it calls the next class in the MRO of the object's actual type. That allows every class in a diamond to run exactly once:
class A:
def greet(self):
print("A")
class B(A):
def greet(self):
print("B")
super().greet()
class C(A):
def greet(self):
print("C")
super().greet()
class D(B, C):
def greet(self):
print("D")
super().greet()
D().greet() # prints D, B, C, A on separate lines
Inside B, super() jumps to C, not A, because C comes next in D's MRO. If B had written A.greet(self) instead, C would be skipped. In other designs, hard-coded parent calls can make the shared base run twice.
When Python Refuses to Build a Class
Some inheritance orders cannot be linearized consistently, and Python raises an error at class creation time:
class X: pass
class Y(X): pass
class Z(X, Y): pass
# TypeError: Cannot create a consistent method resolution order (MRO) for bases X, Y
Listing X before Y asks Python to check X first, but Y is a subclass of X and must come before it. Reversing the order to class Z(Y, X), or simply inheriting from Y alone, fixes the problem.
Mixins in Real Projects, and Pitfalls
Multiple inheritance in everyday Python usually appears as mixins: small classes that add one behaviour, such as logging or JSON export. Libraries like Django and scikit-learn use this pattern widely.
class JsonMixin:
def to_json(self):
import json
return json.dumps(self.__dict__)
class Model:
def __init__(self, name):
self.name = name
class Product(JsonMixin, Model):
pass
print(Product("Laptop").to_json()) # {"name": "Laptop"}
- Put mixins on the left of the main base class so their methods are found first.
- Call
super()consistently in every class that overrides the same method, or the chain breaks. - Accept
**kwargsin cooperative__init__methods so each class can pass on arguments it does not use. - Check
ClassName.mro()whenever a method behaves unexpectedly. It answers "which version runs?" in one line.