Python Multiple Inheritance With Examples

We are going to learn about python multiple inheritance today. Before starting multiple inheritance, I suggest you to skim through Python Class and Python Inheritance if you are not familiar with them.

Python Multiple Inheritance

The name says it all. One class extending more than one class is called multiple inheritance. This is one of the cool specialties of python which makes it more convenient than java in some cases (Java doesn’t support multiple inheritance). Java doesn’t have it because at times multiple inheritance may create some ambiguity. We shall talk about it later in this tutorial.

Multiple Inheritance vs Multi-level Inheritance

It may seem confusing if you are familiar with multi-level inheritance before. The main difference between multiple and multi-level inheritance is that, in multi-level inheritance the superclass may also inherit another super class. And in this way, different levels of inheritance can be created among the classes.


Python Multiple Inheritance Example

Let’s demonstrate a short example of python multiple inheritance.

The classes Person and Student are superclass here and Resident is the subclass. The class Resident extends both Person and Student to inherit the properties of both classes. The example is easily understandable if you have the slightest knowledge of python class and python inheritance. This code yields the following output.

Resolving the Conflicts with python multiple inheritance

Let’s have a look at another example.

Class C inherits both A and B. And both of them has an attribute ‘name’. From which class the value of name will be inherited in C? Is it from A or B? What do you think? Well, the output is:

The name when printed is ‘Richard’ instead of ‘John’. Let’s try to understand what’s happening here. In the constructor of C, the first constructor called is the one of A. So, the value of name in C becomes same as the value of name in A. But after that, when the constructor of B is called, the value of name in C is overwritten by the value of name in B. So, the name attribute of C retains the value ‘Richard’ when printed. The result would be same even if we declared class C as:

The hierarchy becomes completely depended on the order of __init__() calls inside the subclass. To deal with it perfectly, there is a protocol named MRO (Method Resolution Order).

Method Resolution Order (MRO)

Let’s replace the previous code with a slightly modified version.

Can you guess the output? Well, let’s find out.

MRO works in a depth first left to right way. super() in the __init__ method indicates the class that is in the next hierarchy. At first the the super() of C indicates A. Then super in the constructor of A searches for its superclass. If it doesn’t find any, it executes the rest of the code and returns. So the order in which constructors are called here is:
C -> A -> B
If we call print(C.__mro__), then we can see the MRO traceroute.

Once the constructor of A is called and attribute ‘name’ is accessed, it doesn’t access the attribute ‘name’ in B. A better understanding of MRO is a must in order to work with python multiple inheritance.

So this was it for python multiple inheritance, hope to come with more interesting features of python afterwards. Happy Coding!

Reference: Python Doc

By admin

Leave a Reply

%d bloggers like this: