Method Resolution Order (MRO) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When Python looks for a method in classes with multiple inheritance, it follows a specific order called Method Resolution Order (MRO).
We want to understand how the time to find a method grows as the number of classes in the inheritance chain increases.
Analyze the time complexity of Python finding a method using MRO.
class A:
def greet(self):
print("Hello from A")
class B(A):
pass
class C(B):
pass
obj = C()
obj.greet()
This code calls greet on an object of class C, which inherits from B and A. Python searches classes in MRO to find greet.
Identify the steps Python takes to find the method.
- Primary operation: Checking each class in the MRO list for the method.
- How many times: Up to the number of classes in the inheritance chain.
As the number of classes grows, Python checks each class one by one until it finds the method.
| Input Size (number of classes) | Approx. Operations (class checks) |
|---|---|
| 3 | Up to 3 checks |
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
Pattern observation: The number of checks grows directly with the number of classes to search.
Time Complexity: O(n)
This means the time to find a method grows linearly with the number of classes Python must check in the MRO.
[X] Wrong: "Python finds methods instantly no matter how many classes there are."
[OK] Correct: Python checks classes one by one in order, so more classes mean more checks and more time.
Understanding MRO time helps you explain how Python handles multiple inheritance efficiently and why class design matters for performance.
What if Python cached method lookups? How would that change the time complexity?
Practice
Solution
Step 1: Understand MRO purpose
MRO defines the sequence Python follows to find methods in classes with inheritance.Step 2: Compare options
Only The order Python looks for methods in inheritance correctly describes MRO's role in method lookup order.Final Answer:
The order Python looks for methods in inheritance -> Option CQuick Check:
MRO = method lookup order [OK]
- Confusing MRO with loop or import order
- Thinking MRO controls code compilation
- Mixing MRO with unrelated Python features
MyClass in Python?Solution
Step 1: Recall MRO access methods
Python provides__mro__attribute andmro()method to check MRO.Step 2: Identify correct syntax
MyClass.__mro__is a tuple showing MRO;MyClass.mro()is a method returning a list. print(MyClass.__mro__) uses__mro__correctly with print.Final Answer:
print(MyClass.__mro__) -> Option AQuick Check:
Use __mro__ attribute to check MRO [OK]
- Using non-existent get_mro() method
- Forgetting parentheses for mro() method
- Trying to print mro without calling it
class A:
def greet(self):
return 'Hello from A'
class B(A):
def greet(self):
return 'Hello from B'
class C(A):
def greet(self):
return 'Hello from C'
class D(B, C):
pass
print(D().greet())Solution
Step 1: Determine MRO of class D
Class D inherits from B and C. Python uses C3 linearization: D > B > C > A.Step 2: Find first greet method in MRO
Method greet is found first in B, so D().greet() calls B's greet method.Final Answer:
'Hello from B' -> Option AQuick Check:
MRO order picks B's greet first [OK]
- Assuming C's greet is called instead of B's
- Thinking A's greet is called directly
- Expecting an error due to multiple inheritance
class X:
def method(self):
return 'X'
class Y:
def method(self):
return 'Y'
class Z(X, Y):
def method(self):
return super().method()
print(Z().method())Solution
Step 1: Analyze super() in Z.method()
super() calls next method in MRO after Z, which is X.method().Step 2: Check output of X.method()
X.method() returns 'X', so print outputs 'X' with no error.Final Answer:
Output: 'X' (no error) -> Option BQuick Check:
super() calls next in MRO, here X.method() [OK]
- Thinking super() needs explicit class and self
- Expecting output 'Y' instead of 'X'
- Assuming syntax error in print statement
F? class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(C, B): pass class F(D, E): pass
Solution
Step 1: Understand MRO consistency rules
Python requires MRO to be consistent and follow C3 linearization rules.Step 2: Check classes D and E inheritance
D inherits B then C; E inherits C then B. This creates conflicting order for F inheriting D and E.Step 3: Result of conflict
Python raises TypeError for class F due to inconsistent MRO from conflicting parent orders.Final Answer:
TypeError due to inconsistent MRO -> Option DQuick Check:
Conflicting parent order causes TypeError [OK]
- Assuming Python picks one MRO silently
- Ignoring C3 linearization rules
- Trying to list MRO despite conflict
