Python  

How does multiple inheritance work, and what is MRO (Method Resolution Order)?

🔹 Introduction

In Object-Oriented Programming, inheritance means one class can use the features (methods and attributes) of another class. Normally, a class has a single parent, but sometimes a class may need to inherit from two or more parent classes. This is called multiple inheritance.

Although multiple inheritance is powerful, it can also create confusion if both parent classes have the same method name. To solve this confusion, Python uses a rule called Method Resolution Order (MRO). This rule decides which parent’s method should run first.

⚙️ What is Multiple Inheritance?

Multiple inheritance allows a child class to have more than one parent class. This means the child class can reuse features from multiple sources.

Example of Multiple Inheritance:

class Parent1:
    def show(self):
        print("This is Parent1")

class Parent2:
    def show(self):
        print("This is Parent2")

class Child(Parent1, Parent2):
    pass

c = Child()
c.show()

👉 In this example, Child inherits from both Parent1 and Parent2. Since both parents have the show() method, Python must decide which one to use. The answer is Parent1, because it is written first in the inheritance list.

🔄 What is MRO (Method Resolution Order)?

The Method Resolution Order (MRO) is the order in which Python looks for a method when it is called. It tells Python which class to check first, second, and so on.

Python uses an algorithm called C3 Linearization to decide this order. The idea is to make sure the search order is predictable and avoids confusion.

You can check the MRO of any class like this:

print(Child.mro())

Or:

print(Child.__mro__)

🧑‍💻 Example of MRO in Action

class A:
    def process(self):
        print("Class A")

class B(A):
    def process(self):
        print("Class B")

class C(A):
    def process(self):
        print("Class C")

class D(B, C):
    pass

obj = D()
obj.process()

👉 In this example, the MRO for D is:

D → B → C → A → object

So when we call obj.process(), Python first looks in D, then B, then C, and finally A. Since B has the method, it is executed, and the output is:

Class B

📌 Why is MRO Important?

MRO is very important in Python because:

  • It removes ambiguity when multiple parents have the same method name.

  • It ensures Python always follows a consistent order when searching.

  • It helps with the super() function, which relies on MRO to know which class should run next.

🏗️ Example Using super() with MRO

class A:
    def process(self):
        print("Class A")

class B(A):
    def process(self):
        print("Class B")
        super().process()

class C(A):
    def process(self):
        print("Class C")
        super().process()

class D(B, C):
    def process(self):
        print("Class D")
        super().process()

obj = D()
obj.process()

👉 Output:

Class D
Class B
Class C
Class A

Here, the super() function makes the program follow the MRO order. Python automatically decides the correct sequence, which avoids mistakes.

🏁 Summary

Multiple inheritance lets a class inherit features from more than one parent class. However, it can cause confusion if both parents define the same method. Python solves this with MRO (Method Resolution Order), which decides the order of searching methods. By using MRO along with super(), Python ensures that methods are always called in a clear, consistent, and logical way.