What is the method resolution order (MRO) in Python?
MRO is the order in which Python searches a class and its parents when you call a method or read an attribute. Python builds it with the C3 rule, and you can see it in ClassName.__mro__.
Explain it simply
With several parents, Python needs a fixed queue to check for a method. The C3 rule guarantees that a child comes before its parents, and that parents keep the left-to-right order you wrote. So the search never visits a class twice.
super() in Python does not mean "my one parent". It means "the next class in the MRO".
Example
class A:
def hi(self): return "A"
class B(A):
def hi(self): return "B"
class C(A):
def hi(self): return "C"
class D(B, C):
pass
print(D().hi()) # B
print([k.__name__ for k in D.__mro__]) # ['D', 'B', 'C', 'A', 'object']D finds hi in B first because B is listed before C. A comes after both, so the diamond is visited once.
What to say in the interview
"MRO is Python's linearised search order for attributes across a class hierarchy, computed by C3 linearisation. It puts children before parents and keeps the declared order of bases. super() follows this order, which makes cooperative multiple inheritance work."
Common mistakes
- Assuming super() always calls the direct parent. In multiple inheritance it calls the next class in the MRO.