Intermediate LevelPython
How does Python resolve method calls in multiple inheritance using the Method Resolution Order (MRO) and C3 Linearization?
❓ Interview Prompt: Explain Multiple Inheritance, Method Resolution Order (MRO), `ClassName.mro()`, C3 Linearization algorithm, and diamond inheritance problem.
When a class inherits from multiple parent classes, Python uses C3 Linearization to calculate a deterministic search order called MRO (Method Resolution Order):
Diamond Problem Resolution:
MRO guarantees that parent classes are searched in order, and base classes are evaluated ONLY AFTER their derived child classes!
•
Inspect MRO via 'ClassName.mro()' or 'ClassName.__mro__'.
Code Implementation Example
python
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass # Diamond inheritance!
print([cls.__name__ for cls in D.mro()])
# Output: ['D', 'B', 'C', 'A', 'object']💡Inspecting class Method Resolution Order (MRO).
🎯 Interview Key Takeaway
Inspect ClassName.mro() to understand method resolution order in multiple inheritance hierarchies.
#MRO#Multiple Inheritance#C3 Linearization#Intermediate
Back to all Python Questions→