Bird
Raised Fist0
Pythonprogramming~10 mins

Method Resolution Order (MRO) in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Method Resolution Order (MRO)
Define Classes with Inheritance
Create Instance of Child Class
Call Method on Instance
Python Checks MRO List
Search Method in Classes in MRO Order
First Found Method is Used
Method Runs
When you call a method on an object, Python looks for it following a specific order called MRO, checking classes from child to parents.
Execution Sample
Python
class A:
    def greet(self):
        return 'Hello from A'

class B(A):
    pass

class C(B):
    def greet(self):
        return 'Hello from C'

obj = C()
print(obj.greet())
This code shows how Python finds the greet method in class C before checking parents.
Execution Table
StepActionMRO ListMethod Found?Output
1Create instance obj of class C[C, B, A, object]No
2Call obj.greet()[C, B, A, object]Check CYes, greet in C
3Execute greet() from C[C, B, A, object]N/AHello from C
4Print output[C, B, A, object]N/AHello from C
💡 Method greet found in class C, so search stops and method runs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
objundefinedinstance of Cinstance of Cinstance of Cinstance of C
MROundefined[C, B, A, object][C, B, A, object][C, B, A, object][C, B, A, object]
method_greetundefinedundefinedgreet from Cgreet from Cgreet from C
outputundefinedundefinedundefinedHello from CHello from C
Key Moments - 3 Insights
Why does Python use class C's greet method instead of class A's?
Because the execution_table row 2 shows Python checks classes in MRO order starting from C, and finds greet in C first, so it stops searching.
What if class C did not have greet method? Where would Python look next?
Python would continue down the MRO list to class B, then class A, as shown in the concept_flow and execution_table step 2 logic.
Why is object included in the MRO list?
Because all classes in Python inherit from object, the base class, so Python checks it last if method is not found earlier, as shown in the MRO list in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, which class's greet method is found first?
AClass A
BClass B
CClass C
Dobject
💡 Hint
Check the 'Method Found?' column in execution_table row 2.
According to variable_tracker, what is the MRO list after step 1?
A[C, B, A, object]
B[object, A, B, C]
C[A, B, C, object]
D[B, C, A, object]
💡 Hint
Look at the MRO variable values after step 1 in variable_tracker.
If class C did not have greet method, which class would Python check next according to the MRO?
Aobject
BB
CA
DNone, it would error
💡 Hint
Refer to the MRO list in execution_table and concept_flow to see the order after C.
Concept Snapshot
Method Resolution Order (MRO) in Python:
- Determines the order Python looks for methods in inheritance.
- Starts from the child class, then parents, up to object.
- First class with the method stops the search.
- Use __mro__ attribute to see the order.
- Important for multiple inheritance to avoid confusion.
Full Transcript
This visual trace shows how Python finds methods using Method Resolution Order (MRO). When an object calls a method, Python checks the class of the object first, then its parents in a specific order. In the example, class C has a greet method, so Python uses it without checking parents. The MRO list is [C, B, A, object]. If C did not have greet, Python would check B, then A, then object. This order ensures Python knows exactly which method to run, even with multiple inheritance.

Practice

(1/5)
1. What does Method Resolution Order (MRO) in Python determine?
easy
A. The order Python compiles code
B. The order Python executes loops
C. The order Python looks for methods in inheritance
D. The order Python imports modules

Solution

  1. Step 1: Understand MRO purpose

    MRO defines the sequence Python follows to find methods in classes with inheritance.
  2. Step 2: Compare options

    Only The order Python looks for methods in inheritance correctly describes MRO's role in method lookup order.
  3. Final Answer:

    The order Python looks for methods in inheritance -> Option C
  4. Quick Check:

    MRO = method lookup order [OK]
Hint: MRO is about method search order in inheritance [OK]
Common Mistakes:
  • Confusing MRO with loop or import order
  • Thinking MRO controls code compilation
  • Mixing MRO with unrelated Python features
2. Which of the following is the correct way to check the MRO of a class MyClass in Python?
easy
A. print(MyClass.__mro__)
B. print(MyClass.get_mro())
C. print(MyClass.MRO())
D. print(MyClass.mro)

Solution

  1. Step 1: Recall MRO access methods

    Python provides __mro__ attribute and mro() method to check MRO.
  2. Step 2: Identify correct syntax

    MyClass.__mro__ is a tuple showing MRO; MyClass.mro() is a method returning a list. print(MyClass.__mro__) uses __mro__ correctly with print.
  3. Final Answer:

    print(MyClass.__mro__) -> Option A
  4. Quick Check:

    Use __mro__ attribute to check MRO [OK]
Hint: Use ClassName.__mro__ to see MRO tuple [OK]
Common Mistakes:
  • Using non-existent get_mro() method
  • Forgetting parentheses for mro() method
  • Trying to print mro without calling it
3. What will be the output of the following code?
class A:
    def greet(self):
        return 'Hello from A'

class B(A):
    def greet(self):
        return 'Hello from B'

class C(A):
    def greet(self):
        return 'Hello from C'

class D(B, C):
    pass

print(D().greet())
medium
A. 'Hello from B'
B. 'Hello from A'
C. 'Hello from C'
D. Error: Ambiguous method

Solution

  1. Step 1: Determine MRO of class D

    Class D inherits from B and C. Python uses C3 linearization: D > B > C > A.
  2. Step 2: Find first greet method in MRO

    Method greet is found first in B, so D().greet() calls B's greet method.
  3. Final Answer:

    'Hello from B' -> Option A
  4. Quick Check:

    MRO order picks B's greet first [OK]
Hint: MRO checks parents left to right, first method wins [OK]
Common Mistakes:
  • Assuming C's greet is called instead of B's
  • Thinking A's greet is called directly
  • Expecting an error due to multiple inheritance
4. Consider the following code snippet. What is the error and how to fix it?
class X:
    def method(self):
        return 'X'

class Y:
    def method(self):
        return 'Y'

class Z(X, Y):
    def method(self):
        return super().method()

print(Z().method())
medium
A. Error: super() call is ambiguous; fix by specifying class and self
B. Output: 'X' (no error)
C. Output: 'Y' (no error)
D. Error: Missing parentheses in print statement

Solution

  1. Step 1: Analyze super() in Z.method()

    super() calls next method in MRO after Z, which is X.method().
  2. Step 2: Check output of X.method()

    X.method() returns 'X', so print outputs 'X' with no error.
  3. Final Answer:

    Output: 'X' (no error) -> Option B
  4. Quick Check:

    super() calls next in MRO, here X.method() [OK]
Hint: super() calls next method in MRO automatically [OK]
Common Mistakes:
  • Thinking super() needs explicit class and self
  • Expecting output 'Y' instead of 'X'
  • Assuming syntax error in print statement
5. Given the classes below, what is the MRO of class F?
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
hard
A. (F, E, D, B, C, A, object)
B. (F, D, E, B, C, A, object)
C. (F, D, B, C, E, C, B, A, object)
D. TypeError due to inconsistent MRO

Solution

  1. Step 1: Understand MRO consistency rules

    Python requires MRO to be consistent and follow C3 linearization rules.
  2. Step 2: Check classes D and E inheritance

    D inherits B then C; E inherits C then B. This creates conflicting order for F inheriting D and E.
  3. Step 3: Result of conflict

    Python raises TypeError for class F due to inconsistent MRO from conflicting parent orders.
  4. Final Answer:

    TypeError due to inconsistent MRO -> Option D
  5. Quick Check:

    Conflicting parent order causes TypeError [OK]
Hint: Conflicting parent order causes MRO TypeError [OK]
Common Mistakes:
  • Assuming Python picks one MRO silently
  • Ignoring C3 linearization rules
  • Trying to list MRO despite conflict