Python Method Resolution Order

In the multiple inheritance scenario, any specified attribute or method is searched first in the current class. If not found, the search continues into parent classes in depth-first, left to right fashion without searching the same class twice. Searching in this way is called Method Resolution Order (MRO).There are three principles followed by MRO.


  • The first principle is to search for the sub class before going for its base classes. Thus if class B is inherited from A, it will search B first and then goes to A.

  • The second principle is that when a class is inherited from several classes, it searches in the order from left to right in the base classes. For example, if class C is inherited from A and B as class C(A,B), then first it will search in A and then in B.

  • The third principle is that it will not visit any class more than once. That means a class in the inheritance hierarchy is traversed only once exactly.

Understanding MRO gives us clear idea regarding which classes are executed and in which sequence. We can easily estimate the output when several base classes are involved.


Inheritance hierarchy

Inheritance hierarchy with several classes

A Python program to understand the order of execution of methods in several base classes according to MRO.s



inheritance types

Single inheritance

When a child class inherits from only one parent class then it is called single inheritance.


Multiple inheritance

When a child class inherits from multiple parent classes then it is called multiple inheritance


Multilevel Inheritance

In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and grandfather.


Hierarchical Inheritance

When more than one derived classes are created from a single base this type of inheritance is called hierarchical inheritance. In this, there will be a parent (base) class and two child (derived) classes.


Hybrid inheritance

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. The class is derived from the two classes as in the multiple inheritance. However, one of the parent classes is not the base class. It is a derived class.

Hybrid Inheritance combines more than one form of inheritance. It is a blend of more than one type of inheritance.


Multilevel Inheritance


Hierarchical Inheritance


Hybrid Inheritance