Method Resolution Order (MRO) defines:
👉 The order in which Python searches for methods and attributes.
Whenever you call:
obj.method()Python follows a strict lookup sequence.
Think of MRO as:
Search Path for Methods
Python asks:
Where should I look FIRST?
If not found → Where NEXT?
Simple inheritance is easy.
Multiple inheritance = Potential chaos
Example problem:
👉 Two parent classes define SAME method.
Which one should Python call?
✔ MRO solves this deterministically.
class Parent:
def greet(self):
print("From Parent")
class Child(Parent):
pass
c = Child()
c.greet()Output:
From Parent
1. Child Instance
2. Child Class
3. Parent Class
class A:
def greet(self):
print("From A")
class B:
def greet(self):
print("From B")
class C(A, B):
pass
c = C()
c.greet()Output:
From A
Because inheritance order:
class C(A, B)Means:
👉 Search A first → Then B
Python exposes MRO using:
ClassName.__mro__OR
ClassName.mro()print(C.__mro__)Output:
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
✔ Exact search order
Classic multiple inheritance scenario.
A
/ \
B C
\ /
D
class A:
def greet(self):
print("From A")
class B(A):
def greet(self):
print("From B")
class C(A):
def greet(self):
print("From C")
class D(B, C):
pass
d = D()
d.greet()Output:
From B
Let's inspect MRO.
print(D.__mro__)Output:
(D, B, C, A, object)
✔ Python follows this EXACT order.
Python computes MRO using:
C3 Linearization Algorithm
Ensures:
✔ No ambiguity
✔ Consistent ordering
✔ Parent precedence respected
✔ Monotonicity maintained
Python guarantees:
- Child before parents
- Left-to-right inheritance priority
- No duplicate class visits
- Logical consistency
CRITICAL INSIGHT
👉 super() follows MRO.
NOT direct parent.
class A:
def greet(self):
print("From A")
class B(A):
def greet(self):
super().greet()
print("From B")
class C(A):
def greet(self):
super().greet()
print("From C")
class D(B, C):
def greet(self):
super().greet()
print("From D")
d = D()
d.greet()Output:
From A
From C
From B
From D
Even though:
class D(B, C)Execution becomes:
A → C → B → D
✔ Because MRO chain
BAD:
A.greet(self)✔ Breaks MRO
✔ Skips chain
ALWAYS USE:
super()Multiple inheritance ≠ Tree
It becomes:
Linearized Chain
✔ Must inspect MRO
Improper design:
✔ May call same logic twice
✔ Happens if super() not used cooperatively
Illegal inheritance:
class X(A, B)
class Y(B, A) #Conflict✔ Python throws MRO error
Error Example:
TypeError: Cannot create a consistent method resolution order
✔ Use super() ALWAYS
✔ Write cooperative classes
✔ Avoid direct parent calls
✔ Inspect MRO when debugging
✔ Design clean hierarchies
✔ Avoid overly complex inheritance
✔ MRO = Method lookup order
✔ Critical for multiple inheritance
✔ Python uses C3 Linearization
✔ super() respects MRO
✔ Hardcoded calls break chain ❌
✔ Diamond problem solved automatically 🔥
- Print MRO of complex hierarchy
- Create diamond inheritance
- Predict method execution order
- Break MRO intentionally
- Fix using
super() - Add class attributes + test lookup
- Build 4-level inheritance chain