Interview Questions, Answers and Tutorials

Method resolution order (MRO)

Method resolution order (MRO)

Hey there! Are you curious about how Python decides which method to run when you have classes and inheritance? Don’t worry if it sounds confusing, because today we’re going to learn all about it! We’ll talk about something called Method Resolution Order or MRO. But first, let’s break it down into simple pieces.

What is a Method Resolution Order (MRO)? Imagine you have a family tree. In Python, classes and inheritance also form a kind of family tree. When you call a method on an object, Python needs to figure out which method to use if there are multiple methods with the same name. MRO helps Python to decide this by following a specific order.

How does MRO work? Python uses a special algorithm to determine the order in which it should look for methods. This order is called the Method Resolution Order or MRO. Python looks for methods in the current class first, and if it doesn’t find it there, it looks in the parent class, and then in the parent’s parent class, and so on.

Python Code Examples: Let’s take a look at some Python code to understand MRO better:

class A:
    def say_hello(self):
        print("Hello from class A")

class B(A):
    def say_hello(self):
        print("Hello from class B")

class C(A):
    def say_hello(self):
        print("Hello from class C")

class D(B, C):
    pass

obj = D()
obj.say_hello()

In this example, we have four classes: A, B, C, and D. Class D inherits from both classes B and C. When we create an object of class D and call the say_hello() method, Python will follow the MRO to determine which say_hello() method to use.

Output:

Hello from class B

Even though class D inherits from both B and C, Python uses the method from class B because it follows the MRO, which prioritizes the method in the first parent class it encounters.

Practice Questions:

  1. What is the purpose of Method Resolution Order (MRO) in Python?
  2. How does Python decide which method to use when there are multiple methods with the same name in different classes?
  3. Explain the output of the given Python code:

class X:
    def method(self):
        print("X")

class Y(X):
    def method(self):
        print("Y")

class Z(X):
    def method(self):
        print("Z")

class K(Y, Z):
    pass

obj = K()
obj.method()

Solutions:
  1. The purpose of Method Resolution Order (MRO) in Python is to determine the order in which Python searches for methods in classes and their parent classes when there are multiple methods with the same name.
  2. Python uses a specific algorithm to follow the Method Resolution Order (MRO). It starts by looking for the method in the current class, then in the first parent class, then in the parent’s parent class, and so on.
  3. The output of the given Python code will be:

Y

Even though class K inherits from both Y and Z, Python uses the method from class Y because it follows the MRO, which prioritizes the method in the first parent class it encounters.

Now you know all about Method Resolution Order (MRO) in Python! It’s like a roadmap that Python uses to find methods in classes and their parent classes. Keep practicing, and you’ll become a Python pro in no time!