CodeGym /Courses /Python SELF EN /Method Resolution Order (MRO)

Method Resolution Order (MRO)

Python SELF EN
Level 16 , Lesson 5
Available

11.1 Method Resolution Order

Method Resolution Order (MRO) determines the sequence in which Python looks for methods and attributes in the class hierarchy. This is especially important when dealing with multiple inheritance, where a class might inherit attributes and methods from several parent classes.

Basically, there's a strict fixed order (or rather, an algorithm), according to which Python navigates the inheritance tree. This algorithm ensures the correct order of method lookup, which can be described as follows:

The C3 Linearization Algorithm

The C3 Linearization Algorithm defines the MRO by combining:

  • The class itself.
  • The list of parent classes in the order they are specified.
  • The MRO of the parent classes in the same order.

Rules of the C3 Linearization Algorithm

  • Preserve local method order: if class A is specified before class B, all methods of class A should be considered before methods of class B.
  • Maintain order in parent classes: if class A is a parent of class B, then all methods of class A should be considered before methods of class B.
  • Respect inheritance order: if class C is a parent of two or more classes, the order of methods in class C should be preserved in the MRO of all these classes.

Algorithm Steps:

Step 1. Start with the class itself:

Always start with the class in which the method is called.

Step 2. Add base classes in the order they are listed:

After the current class, check the base classes in the order they are listed during inheritance.

Step 3. Navigate through parent classes:

Look for fields and methods there.

Step 4. Merge parent classes' MRO:

If the same base class is inherited through multiple paths, it is checked only once and in the correct order (all other times it will be skipped).

For those familiar with the "Algorithms and Data Structures" topic, this is a depth-first search, not a breadth-first.

11.2 Checking MRO

In Python, you can check the method and field traversal order of a class using the __mro__ attribute or the mro() function.

Example:


class A:
    def method(self):
        print("A")
        
class B(A):
    def method(self):
        print("B")
        
class C(A):
    def method(self):
        print("C")
        
class D(B, C):
    def method(self):
        print("D")
        

# Checking MRO
print(D.__mro__)
        

The output will be:


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

This shows the order in which Python will look for methods and attributes:

  • D: Python checks the method in class D first.
  • B: Then Python checks the method in class B (the first parent class).
  • C: If the method is not found in class B, Python checks the method in class C (the second parent class).
  • A: If the method is not found in classes B and C, Python checks the method in class A.
  • object: Finally, Python checks the method in the base class object.

11.3 Using super() with MRO

The super() function follows the MRO to call methods of parent classes in the correct order. Let's consider an example of using super():

class A:
    def method(self):
        print("A")
        super().method()
        
class B(A):
    def method(self):
        print("B")
        super().method()
        
class C(A):
    def method(self):
        print("C")
        super().method()
        
class D(B, C):
    def method(self):
        print("D")
        super().method()
        
        
d = D()
d.method()
        

The output will be:


D
B
C
A
        

Traversal Order (MRO)

1. Calling the method in class D:

  • Python first checks the method in class D and finds it there.
  • Method D.method() is executed and prints "D".
  • Then super().method() is called, which follows the MRO to call the next method.

2. Calling the method in class B:

  • According to the MRO, the next class after D is B.
  • Method B.method() is executed and prints "B".
  • Then super().method() is called, which follows the MRO to call the next method.

3. Calling the method in class C:

  • The next class in the MRO after B is C.
  • Method C.method() is executed and prints "C".
  • Then super().method() is called, which follows the MRO to call the next method.

4. Calling the method in class A:

  • The next class in the MRO after C is A.
  • Method A.method() is executed and prints "A".
  • Then super().method() is called, but since A has no parent methods method (except object), the call completes with no further actions.
2
Task
Python SELF EN, level 16, lesson 5
Locked
Using super() and MRO
Using super() and MRO
2
Task
Python SELF EN, level 16, lesson 5
Locked
Method Overriding
Method Overriding
1
Опрос
Inheritance,  16 уровень,  5 лекция
недоступен
Inheritance
Inheritance
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION