Site icon DataFlair

Python Multiple Inheritance – Python MRO (Method Resolution Order)

Python course with 57 real-time projects - Learn Python

In our last article, we discussed Python inheritance. Here, Python Multiple Inheritance tutorial, we will discuss what is Multiple inheritances in Python with its examples and uses.

On the other hand, we will learn Python MRO (Method Resolution Order). At last, we will learn complications in Multiple Inheritance in Python Programming Language.

So, let’s start Python Multiple Inheritance Tutorial.

Python Multiple Inheritance

What is Python Multiple Inheritance?

As its name is indicative, multiple inheritance in python is when a class inherits from multiple classes. One example of this would be that a child inherits personality traits from both parents.

Python Multiple Inheritance – Example

Before we proceed to multiple inheritance syntaxes, let’s see the python syntax.

Python Multiple Inheritance Syntax

To make a class inherit from multiple python classes, we write the names of these classes inside the parentheses to the derived class while defining it.

We separate these names with commas.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

The code for the previous example would be:

>>> class Mother:
                pass
>>> class Father:
                pass
>>> class Child(Mother,Father):
                pass
>>> issubclass(Child,Mother) and issubclass(Child,Father)

Output

True

Python MRO (Method Resolution Order)

When we search for an attribute in a class that is involved in python multiple inheritance, an order is followed.

First, it is searched in the current class. If not found, the search moves to parent classes. This is left-to-right, depth-first.

So, in the above class, the search order will be – Child, Mother, Father, Object.

This order is called linearization of class Child, and the set of rules applied are called MRO (Method Resolution Order).

To get the MRO of a class, you can use either the __mro__ attribute or the mro() method.

>>> Child.__mro__

Output

(<class ‘__main__.Child’>, <class ‘__main__.Mother’>, <class ‘__main__.Father’>, <class ‘object’>)

The __mro__ attribute returns a tuple, but the mro() method returns a python list.

>>> Child.mro()

Output

[<class ‘__main__.Child’>, <class ‘__main__.Mother’>, <class ‘__main__.Father’>, <class ‘object’>]

To take a more complex example that also demonstrates depth-first search, we take 6 classes.

>>> class X:pass
>>> class Y: pass
>>> class Z:pass
>>> class A(X,Y):pass
>>> class B(Y,Z):pass
>>> class M(B,A,Z):pass
>>> M.mro()

Output

[<class ‘__main__.M’>, <class ‘__main__.B’>, <class ‘__main__.A’>, <class ‘__main__.X’>, <class ‘__main__.Y’>, <class ‘__main__.Z’>, <class ‘object’>]

We can represent this with the following diagram.

Python Multiple Inheritance – Method Resolution order (MRO)

First, the interpreter scans M. Then, it scans B, and then A-B first because of the order of arguments at the time of inheritance.

It scans Z later, after X and Y. The order is- X, then Y, then Z. This is because due to depth-first search, X comes first to A.

Finally, it scans the class object. Hence, the order.

Complications in Python Multiple Inheritance

What happens when the classes we inherit from all have a common attribute? Whose value does the child class take, then?

Let’s take three classes A, B, and C.

>>> class A:
                id=1            
>>> class B:
                id=2             
>>> class C:
                id=3              
>>> class M(A,C,B):
                pass
>>> M.id

Output

1
>>> M.id

Output

1
>>> class M(C,B,A):
                pass
>>> M.id

Output

3

Like we see here, the class named first in the inheritance passes its value to the child class for the common attribute.

Earlier, it was A, so, M had id=1. Then, when we changed it to C, M got id=3. This is the same with Python methods/functions of the class.

>>> class A:
                def sayhi():
                                print("A")                          
>>> class B:
                def sayhi():
                                print("B")                         
>>> class M(A,B):
                pass
>>> M.sayhi()

Output

A

So, this was all about Python Multiple Inheritance Tutorial. Hope you like our explanation on Python MRO.

Python Interview Questions on Multiple Inheritance

  1. How do you achieve multiple inheritance in Python?
  2. When should we use multiple inheritance in Python?
  3. What are the advantages and disadvantages of Python multiple inheritance?
  4. What is Python multiple inheritance? Explain with an example.
  5. How does Python multiple inheritance work?

Conclusion

Hence, in this tutorial, we discussed python multiple inheritances and its syntax and examples.

We also talked about Method Resolution Order(MRO) and last we discuss, complications in multiple inheritances in python.

Keep going with us in Python and give us your valuable feedbacks.

Exit mobile version