Method Resolution Order In Python

Ridwan Yusuf
3 min readJan 13, 2023

--

In Object Oriented Programming(OOP), a child class inherits from a parent class and it’s able to access all attributes and methods provided by
the parent class. When a method which has already been defined in a parent class is redefined in the child class, the child class overrides(Method Overriding) the method defined in the parent/super class.

Take a look at this simple snippet:
[*]Snippet in Python

class Animal:
def move(self):
print('Animal is moving')
class Cat(Animal):
def move(self):
print('Cat is moving')

Up here, the parent class is Animal and it has a method move() which was defined in the child/sub-class Cat as well. The method defined in the
the child overrides the one in the Parent. This is know as method overriding.

[*]Usage

cat1 = Cat()
cat1.move()

[*]Output
Cat is moving

Calling the move() on instance/object cat1 reveals that the method that was called is in the Cat class(Cat inherits from Animal class).

For languages that support only single inheritance(i.e. a class can only inherits from a parent class), it is understandable that whenever a method is invoked, it looks for it first in the child before searching further in its parent.

For languages supporting multiple inheritance(where a class can inherits from many classes), knowing the order in which methods are searched for is essential. Python supports multiple inheritance

class Parent1:
def move(self):
print('Move method from Parent1')

class Parent2:
def move(self):
print('Move method from Parent2')
class Parent3:
def move(self):
print('Move method from Parent3')

class Child(Parent1, Parent2, Parent3):
def another_method(self):
print('Another method')

Methods are searched from bottom to top(i.e. from a base class to the parent class) and left to right.
Going by the example here where class Child inherits from three different methods.
The first place it searches is the Child class (bottom). If the method is found in Child. It stops and make use of it.
If it is not found in the Child class, it then moves from left to right searching for it. In this case, it moves to Parent1, then Parent2 and
then Parent3.

[*]Usage

cat1 = Child()
cat1.move()

[*]Output
Move method from Parent1

This explains why the order in which we list out the parent classes in the child is important. Let’s change the order an see what happens

class NewChild(Parent3, Parent1, Parent2):
def another_method(self):
print('print sth')

[*]Usage

cat2 = NewChild()
cat2.move()

[*]Output
Move method from Parent3

[*NOTE] When a method is not found in the child class,it moves to the first parent for searching.If it is not found in the first parent and that first parent also inherits from another class, it goes to the first parent’s parent before moving to search the second parent.

class Master():
def move(self):
print('Moving from the parent of Parent2 i.e. Master')

class Parent1:
pass

class Parent2(Master):
pass

class Parent3:
def move(self):
print('Move method from Parent3')

class Child(Parent1, Parent2, Parent3):
def another_method(self):
print('Another method')

It searches the Child for the move() method but not found. It moves to Parent1 not found. Then to Parent2, still not found. Since Parent2 is inheriting
from another class, it goes up to the class first before moving to Parent3
[*]Usage

cat3 = Child().move()

[*]Output
Moving from the parent of Parent2 i.e. Master

Finally, Python provides a built-in which you can use to know the order in which methods will be searched for in a class by using
mro() method or __mro__ attribute
[*]Usage
print(Child.__mro__)

[*]Output
(<class ‘__main__.Child’>, <class ‘__main__.Parent1’>, <class ‘__main__.Parent2’>, <class ‘__main__.Master’>, <class ‘__main__.Parent3’>, <class ‘object’>)

This shows that Master comes before Parent3.

Ready to elevate 🚀 your Python 🐍 skills? Check out YouTube for exclusive content. Consider subscribing to stay updated.

--

--

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use