CodeCraft
← Back to Blog
code tech2026-04-13·10 min read

Understanding Python MRO (Method Resolution Order) — From Theory to Practice

#python#mro#multiple inheritance#c3 linearization#oop
C
CodeCraft Team· Engineering

If you're dealing with interviews, framework development, or reading complex source code, understanding MRO is essential.


1. What is MRO?

MRO (Method Resolution Order) refers to:

The order in which Python searches for a method or attribute in a class hierarchy.


Example

class A:
    def hello(self):
        print("A")

class B(A):
    pass

class C(A):
    def hello(self):
        print("C")

class D(B, C):
    pass

d = D()
d.hello()

Output:

C

Why is the result "C"?

Because Python follows the MRO order:

D → B → C → A → object

It finds the hello() method in class C and stops searching.


2. How to Check MRO?

Python provides two ways:

print(D.__mro__)

or:

print(D.mro())

Output:

(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

3. Why Do We Need MRO?

MRO is mainly used to resolve:

👉 Method conflicts in multiple inheritance

For example:

class A:
    def hello(self):
        print("A")

class B(A):
    pass

class C(A):
    def hello(self):
        print("C")

class D(B, C):
    pass

The question is:

👉 Which hello() should D().hello() call?

  • A?
  • C?
  • B?

👉 MRO provides a clear and deterministic rule to resolve this ambiguity.


4. Core Algorithm: C3 Linearization

Python uses:

C3 Linearization

This algorithm ensures a consistent and predictable order of inheritance.


Three Key Principles of C3

1️⃣ Child-first

The child class always comes before its parents:

D → B → C → ...

2️⃣ Preserve Declaration Order

class D(B, C):
    pass

👉 B always comes before C


3️⃣ Monotonicity

The order of parent classes is preserved and will not be altered in subclasses.


5. Classic Examples

Example 1: Basic Multiple Inheritance

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

MRO:

D → B → C → A → object

Example 2: Changing Inheritance Order

class D(C, B):
    pass

MRO:

D → C → B → A → object

👉 Conclusion: Inheritance order directly affects MRO


6. super() and MRO (Important 🔥)

Many people think:

super() calls the “parent class”

❌ Incorrect!

👉 The truth is:

super() follows the MRO and calls the next class in the chain


Example

class A:
    def hello(self):
        print("A")

class B(A):
    def hello(self):
        print("B")
        super().hello()

class C(A):
    def hello(self):
        print("C")
        super().hello()

class D(B, C):
    def hello(self):
        print("D")
        super().hello()

D().hello()

Output:

D
B
C
A

Explanation

MRO:

D → B → C → A → object

Call chain:

D → B → C → A

👉 super() follows MRO, not just direct parent classes.


7. A Common Pitfall (Interview Trap)

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

👉 This code raises an error:

TypeError: Cannot create a consistent method resolution order

Why?

Because:

  • D requires: B before C
  • E requires: C before B

👉 Conflict occurs, and C3 cannot resolve a consistent order.


8. Best Practices

✅ 1. Avoid Complex Multiple Inheritance

  • Reduces complexity
  • Prevents MRO conflicts

✅ 2. Always Use super()

super().method()

Advantages:

  • Works with MRO
  • Safer and more maintainable
  • Better extensibility

❌ Not Recommended

A.method(self)

👉 Breaks the MRO chain


✅ 3. Inspect MRO When Debugging

print(ClassName.mro())

👉 A powerful debugging tool for inheritance issues


9. Summary

👉 One-line summary:

MRO (Method Resolution Order) defines how Python searches for methods in a multiple inheritance hierarchy, using the C3 linearization algorithm to ensure consistency and predictability.


10. Interview Answer

Python's MRO (Method Resolution Order) determines the order in which methods are resolved in multiple inheritance scenarios. It uses the C3 linearization algorithm to ensure child-first ordering, preserve inheritance order, and maintain monotonicity. Method lookup follows the MRO from left to right.


Further Reading

  • Python Official Documentation: MRO
  • C3 Linearization Paper
  • Multiple inheritance design in Django / Flask source code