Method Resolution Order in Python Inheritance (original) (raw)

Last Updated : 15 Jan, 2026

Method Resolution Order (MRO) defines the order in which Python searches for a method in a class and its parent classes. It becomes important when the same method exists in more than one class in an inheritance chain, especially in multiple inheritance.

he example shows how Python decides which method to execute when both a parent and a child class have a method with the same name.

Python `

class A: def fun(self): print("In class A")

class B(A): def fun(self): print("In class B")

a = B() a.fun()

`

**Explanation:

Multiple Inheritance (Diamond Problem)

In multiple inheritance, a child class can inherit from more than one parent class. When these parent classes come from a common base class, the structure forms a diamond shape and Python must decide which class method should be used.

Python `

class A: def fun(self): print("In class A")

class B(A): def fun(self): print("In class B")

class C(A): def fun(self): print("In class C")

class D(B, C): pass

a = D() a.fun()

`

**Explanation:

C3 Linearization in MRO

Python uses the C3 linearization algorithm to decide the order in which classes are searched when a method is called. This algorithm produces a single, consistent order that respects both inheritance and the order in which parent classes are written.

**Example: This program prints the exact order Python uses for method lookup.

Python `

class A: def fun(self): print("In class A")

class B(A): def fun(self): print("In class B")

class C(A): def fun(self): print("In class C")

class D(B, C): pass

obj = D() obj.fun()

print(D.mro)

`

Output

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

**Explanation:

Methods to View Method Resolution Order (MRO) of a Class

Python provides two ways to check the method resolution order (MRO) of a class:

class A: def fun(self): print("In class A")

class B: def fun(self): print("In class B")

class C(A, B): def init(self): print("Constructor C")

obj = C()

print(C.mro) print(C.mro())

`

Output

Constructor C (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>) [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]

**Explanation: