MRO, Method Resolution Order
The MRO algorithm
- List the classes that an instance inherits from using the classic class’s DFLR(Depth First, Left to Right) lookup rule, and include a class multiple times if it’s visited more than once.
- Scan the resulting list for duplicate classes, removing all but the last occurrence of duplicates in the list.
Tracing the MRO
>>> class A(object): attr = 1 >>> class B(A): pass >>> class C(A): attr = 2 >>> class D(B, C): pass >>> D.__mro__ (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
>>> class A(object): attr = 1 >>> class B(A): pass >>> class C(object): attr = 2 >>> class D(B, C): pass >>> D.__mro__ (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <type 'object'>)