Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Method Resolution Order in Python: How Python Solves the Diamond Problem

5 min readAug 21, 2022

--

What kind of order is a method resolution order?

It is the order in which Python searches for a method in a class hierarchy. It is beneficial when we are dealing with multiple inheritances. The importance of method resolution isn’t apparent until we are dealing with a situation where we absolutely need it.

For non-members, click here to read the blog.

Where is it needed exactly?

Suppose we have the following situation. We have a father class and a mother class. And we have a child class that inherits from both father and mother in that order.

In this case, which function will be called when child.do_something() is called since both the father and the mother have the function do_something? This is exactly the type of situation where we need to linearize the order of classes in the hierarchy to determine whose methods or attributes take precedence over the other.

How to get the MRO of a class?

To display the method resolution order of a class, we can either access the __mro__ attribute of the class or call the Child.mro() function. This will give method resolution order. From this, we can see that the…

--

--

Diwash Tamang
Diwash Tamang

Written by Diwash Tamang

Geomatics Engineer | Python Developer | Lifelong Learner | Aspiring Blogger | Pursuit of Excellence

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development