Method resolution order in Python Inheritance
Last Updated :
06 Mar, 2025
MRO stands for Method Resolution Order. It is the order in which Python looks for a method in a hierarchy of classes. MRO follows a specific sequence to determine which method to invoke when it encounters multiple classes with the same method name.. For Example :
Python
# Python program showing
# how MRO works
class A:
def rk(self):
print("In class A")
class B(A):
def rk(self):
print("In class B")
r = B()
r.rk()
In the above example the methods that are invoked is from class B but not from class A, and this is due to Method Resolution Order(MRO).
The order that follows in the above code is- class B – > class A .
In multiple inheritances, the methods are executed based on the order specified while inheriting the classes. For the languages that support single inheritance, method resolution order is not interesting, but the languages that support multiple inheritance method resolution order plays a very crucial role. Let’s look over another example to deeply understand the method resolution order:
Python
class A:
def rk(self):
print("In class A")
class B(A):
def rk(self):
print("In class B")
class C(A):
def rk(self):
print("In class C")
# classes ordering
class D(B, C):
pass
r = D()
r.rk()
In the above example we use multiple inheritances and it is also called Diamond inheritance and it looks as follows:

Python follows a depth-first lookup order and hence ends up calling the method from class A. By following the method resolution order, the lookup order as follows.
Class D -> Class B -> Class C -> Class A
Python follows depth-first order to resolve the methods and attributes. So in the above example, it executes the method in class B.
Old and New Style Order :
In the older version of Python(2.1) we are bound to use old-style classes but in Python(3.x & 2.2) we are bound to use only new classes. New style classes are the ones whose first parent inherits from Python root ‘object’ class.
Python
# Old style class
class OldStyleClass:
pass
# New style class
class NewStyleClass(object):
pass
Method resolution order(MRO) in both the declaration style is different. Old style classes use DLR or depth-first left to right algorithm whereas new style classes use C3 Linearization algorithm for method resolution while doing multiple inheritances.
DLR Algorithm
During implementing multiple inheritances, Python builds a list of classes to search as it needs to resolve which method has to be called when one is invoked by an instance. As the name suggests, the method resolution order will search the depth-first, then go left to right. For Example:
Python
class A:
pass
class B:
pass
class C(A, B):
pass
class D(B, A):
pass
class E(C,D):
pass
In the above Example algorithm first looks into the instance class for the invoked method. If not present, then it looks into the first parent, if that too is not present then-parent of the parent is looked into. This continues till the end of the depth of class and finally, till the end of inherited classes. So, the resolution order in our last example will be D, B, A, C, A. But, A cannot be twice present thus, the order will be D, B, A, C. But this algorithm varying in different ways and showing different behaviours at different times .So Samuele Pedroni first discovered an inconsistency and introduce C3 Linearization algorithm.
C3 Linearization Algorithm :
C3 Linearization algorithm is an algorithm that uses new-style classes. It is used to remove an inconsistency created by DLR Algorithm. It has certain limitation they are:
- Children precede their parents
- If a class inherits from multiple classes, they are kept in the order specified in the tuple of the base class.
C3 Linearization Algorithm works on three rules:
- Inheritance graph determines the structure of method resolution order.
- User have to visit the super class only after the method of the local classes are visited.
- Monotonicity
Methods for Method Resolution Order(MRO) of a class:
To get the method resolution order of a class we can use either __mro__ attribute or mro() method. By using these methods we can display the order in which methods are resolved. For Example
Python
# Python program to show the order
# in which methods are resolved
class A:
def rk(self):
print("In class A")
class B:
def rk(self):
print("In class B")
# classes ordering
class C(A, B):
def __init__(self):
print("Constructor C")
r = C()
# it prints the lookup order
print(C.__mro__)
print(C.mro())
Similar Reads
Python | os.set_inheritable() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.set_inheritable() method in Python is used to set the value of inheritable fla
2 min read
Instance method in Python
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
2 min read
Python | os.get_inheritable() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.get_inheritable() method in Python is used to get the value of inheritable fla
2 min read
Multiple Inheritance in Python
Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class
5 min read
Types of inheritance Python
Inheritance is defined as the mechanism of inheriting the properties of the base class to the child class. Here we a going to see the types of inheritance in Python. Types of Inheritance in Python Types of Inheritance depend upon the number of child and parent classes involved. There are four types
3 min read
Inheritance in Python
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this arti
7 min read
isinstance() method - Python
isinstance() is a built-in Python function that checks whether an object or variable is an instance of a specified type or class (or a tuple of classes), returning True if it matches and False otherwise, making it useful for type-checking and ensuring safe operations in dynamic code. Example: [GFGTA
3 min read
Python Multiple Inheritance With super() Function
We are given some classes and our task is to find out how super() function works with multiple inheritance in Python. In this article, we will see how Python super() works with Multiple Inheritance. Python Multiple Inheritance With super() Below, are examples of how Python's Super() Work with Multip
2 min read
Python | super() in single inheritance
Prerequisites: Inheritance, function overriding At a fairly abstract level, super() provides the access to those methods of the super-class (parent class) which have been overridden in a sub-class (child class) that inherits from it. Consider the code example given below, here we have a class named
6 min read
Python | super() function with multilevel inheritance
super() function in Python: Python super function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by 'super'. To understand Python super function we m
3 min read