Method Resolution Order (MRO) in Python

Method Resolution Order (MRO) is a fundamental concept in Python, particularly relevant when dealing with class inheritance. It defines the sequence in which Python searches for a method in a class hierarchy. This order is crucial to ensure that the correct method is executed when a method is called on an instance of a class, especially in scenarios involving multiple inheritance.

Overview of MRO

In Python, the MRO follows a depth-first, left-to-right search pattern. This means that when a method is called, Python will first look for it in the current class, then move to the leftmost base class, and continue this process until it finds the method. If a method is not found, Python will backtrack and search the next base class. Importantly, MRO removes duplicates and ensures that each class appears after its subclasses in the order. This systematic approach ensures that the correct method is executed, avoiding potential conflicts in multiple inheritance scenarios.

How MRO Works

When you call a method on an instance, Python follows a specific order to resolve the method:

  1. Check the Instance's Class: Python first checks if the method is implemented in the instance's class.
  2. Move to Superclass: If the method is not found, Python moves up to the superclass of the instance's class.
  3. Check the object Class: If the method is still not found, Python checks the object class, which is the ultimate superclass for all classes in Python.
  4. Raise AttributeError: If the method is not found in any of these classes, Python raises an AttributeError exception.

This process is repeated for every level in the class inheritance structure until the method is resolved or all possibilities are exhausted.

Example of MRO: The Liger Class

Consider the Liger class, which inherits from both Lion and Tiger. When the method super().eats() is called within the Liger class, Python uses the MRO to determine the order of method execution. The MRO for the Liger class is as follows: Liger, Lion, Tiger, BigCat, object.

When super().eats() is invoked, Python will traverse this order to find the eats() method. It first encounters the Lion class, which provides the method and returns ['wildebeest']. The Liger class then adds its own list of prey animals to this result, demonstrating how MRO influences method execution and output.

Inspecting MRO

You can inspect the MRO of a class using the mro method. This method returns a list of classes that represent the order in which methods are resolved. For example:

Supervisor.mro()

# [Output:](https://livebook.manning.com/book/python-how-to/chapter-8/298)
[<class '__main__.Supervisor'>, <class '__main__.Employee'>, <class 'object'>]

In this example, the resolution order is Supervisor -> Employee -> object. This means that if a method is called on an instance of Supervisor, Python will first look for the method in Supervisor, then in Employee, and finally in object. If the method is not found in any of these classes, an AttributeError will be raised.

MRO in Multiple Inheritance

Python supports multiple inheritance, where a class can inherit from multiple classes. This makes the MRO more complex. However, the most common scenario involves a subclass with only one superclass. In such cases, the MRO is straightforward. When a class does not explicitly define a superclass, Python uses the object class as its default superclass.

Visual Representation of MRO

The process of determining the MRO for a class can be visualized in the following figures:

Figure 8.7: How Python determines the method resolution order for a class Figure 8.7. How Python determines the method resolution order for a class

Figure 8.10: MRO in a hierarchical class structure Figure 8.10 MRO in a hierarchical class structure

These figures illustrate the sequence and logic Python uses to establish the MRO, ensuring that methods are resolved correctly in complex inheritance hierarchies.

| Book Title | Usage of Method Resolution Order | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|----------------------------------|-----------------|------------------------------|---------------|-----------------------|
| [Practices of the Python Pro](https://www.manning.com/books/practices-of-the-python-pro) | Discusses MRO in the context of multiple inheritance, emphasizing its role in determining method execution order. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-8/92) | Provides a detailed explanation of MRO's depth-first, left-to-right search pattern and its importance in avoiding conflicts. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-8/92) | Connects MRO to the concept of inheritance hierarchies and method execution. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-8/92) | Uses the `Liger` class example to illustrate MRO in action. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-8/75) | Highlights the practical implications of MRO in method execution and conflict resolution. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-8/92) |
| [Python How-To](https://www.manning.com/books/python-how-to) | Explains MRO as a crucial concept for method and attribute evaluation in inherited class structures. [more](https://livebook.manning.com/book/python-how-to/chapter-8/287) | Describes the step-by-step process of MRO, including checking the instance's class, superclass, and `object` class. [more](https://livebook.manning.com/book/python-how-to/chapter-8/298) | Links MRO to the broader topic of class inheritance and method resolution. [more](https://livebook.manning.com/book/python-how-to/chapter-8/298) | Provides an example using the `Supervisor` class to demonstrate MRO inspection. [more](https://livebook.manning.com/book/python-how-to/chapter-8/298) | Discusses MRO's role in handling multiple inheritance and resolving method calls. [more](https://livebook.manning.com/book/python-how-to/chapter-8/298) |

FAQ (Frequently asked questions)