Sub topic 9
Reading the method resolution order
This chapter assumes you can write subclasses, override methods, and call super(). It also assumes you know that Python looks up a method on the instance's class and then on its parents. Here you will learn the exact order of that search and how to read it.
What the MRO is
The method resolution order (MRO) is the list of classes Python searches, in order, when you access an attribute or method on an instance. The first class in the list that defines the name wins. With single inheritance the list is a straight line from the class up to object. With multiple inheritance Python has to decide how to merge several lines into one.
Every class stores its MRO in __mro__. You can also call ClassName.mro() to get it as a list.
class A:
def hello(self):
return ["A"]
class B(A):
def hello(self):
return ["B"] + super().hello()
class C(A):
def hello(self):
return ["C"] + super().hello()
class D(B, C):
def hello(self):
return ["D"] + super().hello()
print([k.__name__ for k in D.__mro__]) # ['D', 'B', 'C', 'A', 'object']
print(D().hello()) # ['D', 'B', 'C', 'A']The hello() result follows the same order as __mro__. Reading __mro__ therefore tells you which method runs and in what sequence super() calls happen.
The two rules behind the order
Python builds the MRO with the C3 linearization algorithm. You don't need to run it by hand. Two rules describe what it guarantees:
- A class always appears before its own parents.
- The order in which you list the bases is preserved. In
class D(B, C),Bcomes beforeC.
When these rules can be satisfied together, the MRO is the result. When they cannot, Python refuses to create the class.
Predicting which method runs
To predict a lookup, write out the MRO and take the first class that defines the name. Parent-before-child is not the whole story. A sibling can be searched before a shared parent.
class A:
def who(self):
return "A"
class B(A):
pass # does not define who()
class C(A):
def who(self):
return "C"
class D(B, C):
pass
print([k.__name__ for k in D.__mro__]) # ['D', 'B', 'C', 'A', 'object']
print(D().who()) # CYou might expect B to pass the call straight up to A. It does not. C sits between B and A in the MRO, so C.who is found first.
What super() really follows
super() does not mean "my parent class." It means "the next class after the current one in the MRO of type(self)." In the diamond above, super() inside B reaches C when the instance is a D. A plain B() instance has the MRO B, A, object, so there super() inside B reaches A.
When Python rejects a hierarchy
If your base order contradicts the inheritance relationships, no valid MRO exists. Listing a parent before its own child is the usual cause.
class A:
pass
class B(A):
pass
try:
class Broken(A, B): # A before B, but B must come before A
pass
except TypeError as exc:
print("TypeError:", exc)The error message reads Cannot create a consistent method resolution order (MRO) for bases A, B. Swap the bases to (B, A) to fix it.
Common mistakes
- Assuming the MRO is a depth-first walk up the left-most parent. In a diamond, that would visit
AbeforeC, which Python never does. - Reading
super()as "the parent written in theclassline." It follows the MRO of the instance's actual class. - Forgetting that
objectis always last.
Exercise
Given class A: pass, class B(A): pass, class C(A): pass, and class D(C, B): pass, what is the MRO of D, listed by class name?
Show answer
D, C, B, A, object. The bases are listed as (C, B), so C comes before B, and the shared parent A comes after both of them.