In Python, Method Resolution Order (MRO) determines the sequence in which base classes are searched when calling a method on an object. This is especially important in multiple inheritance, where a class inherits from multiple parent classes.
In this tutorial, we will cover:
super() function and MROMRO defines the order in which Python looks for methods when a method is called on an object. It ensures:
Consistency in method calls
Avoids redundant calls in multiple inheritance
Prevents infinite loops
Example
class A:
def show(self):
print("A")
class B(A):
pass
class C(A):
def show(self):
print("C")
class D(B, C): # Multiple Inheritance
pass
d = D()
d.show() # Output: CPython looks for show() in D → B → C → A (Left to Right).
The first occurrence of show() is in C, so C.show() is executed.
Check MRO using __mro__ or mro()
print(D.__mro__)
# Output: (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)MRO follows a depth-first, left-to-right approach, but it ensures that:
object class is always at the end.Example
class X:
def show(self):
print("X")
class Y(X):
def show(self):
print("Y")
class Z(X):
pass
class M(Y, Z):
pass
m = M()
m.show() # Output: YMRO: M → Y → Z → X → objectshow() is found in Y, so Y.show() is executed.
Check MRO:
print(M.mro())
# Output: [<class '__main__.M'>, <class '__main__.Y'>, <class '__main__.Z'>, <class '__main__.X'>, <class 'object'>]Python uses the C3 Linearization algorithm (or C3 MRO) to determine MRO in multiple inheritance.
C3 MRO ensures:
Example of C3 MRO
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.mro())
# Output: [D, B, C, A, object]How C3 Works?
D → Look at B and C → Then A → Finally, object.B) is checked before C.super() Function and MROThe super() function follows MRO to determine which method to call.
Example: Using super() in multiple inheritance
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
super().show()
class C(A):
def show(self):
print("C")
super().show()
class D(B, C):
def show(self):
print("D")
super().show()
d = D()
d.show()Output:
D
B
C
AD.show() is executed first.super().show() in D calls B.show().super().show() in B calls C.show().super().show() in C calls A.show().A.show() does not call super(), so execution stops.
The diamond problem occurs when a class inherits from two classes that have a common parent.
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A):
def show(self):
print("C")
class D(B, C): # Diamond inheritance
pass
d = D()
d.show() # Output: BMRO: D → B → C → A → object
D checks B first, so B.show() is executed.A is visited only once (C3 MRO ensures this).MRO determines the order in which methods are called in multiple inheritance
Python follows C3 Linearization (C3 MRO algorithm)
Use __mro__ or mro() to check MROsuper() follows MRO and avoids redundant calls
MRO prevents the Diamond Problem by ensuring a single occurrence of each class
Sign in to join the discussion and post comments.
Sign inPython Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.
Python for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.