In my previous post, I discussed about Classes and Object Oriented Programming Concepts in Python. We also mentioned and took an example of Inheritance which displayed how does multi-level inheritance takes place in Python. Although, we did not mention anything about multiple inheritance in Python. We will do that now.

The concept of multiple inheritance or its applicability on any Python code depends on the type of the Python Class that is being used. So far, in the previous post what we studied was Modern Python Classes but there is also another primitive type of Python class which is called Classical Python Classes. Thus, in order to understand the multiple inheritance and order of method resolution in it, we need to start with the study of these two types of Python Classes. Let’s dive into it.

Classical Python Classes vs. Modern (New) Python Classes

Classical ClassModern Class
Not available in Python 3.Available in Python 3. In fact, in Python 3, only modern python classes are supported.
Declaration class A:Declaration class A(object):
No built-in base class type available.All the modern classes have a built-in base class called object.
Multi-level and Multiple Inheritance both Supported.Only Multi-level inheritance supported.

Hope that classical and modern python classes are clear now. So, now we get back to Method Resolution Order (MRO), which would be only applicable in classical python classes due to the diamond problem. Thus, we will understand the method resolution order by taking the examples of classical python classes only. Remember, that, this concept of MRO is only applicable up to Python 2.7 as Python 3 does not support classical python classes.

Consider the following example:

Click here to Copy the Code

Here’s the output of the above program:

From the output, we can see that the method of class D is being called first as expected. This is because class D is at the lowest hierarchy of the classes where it inherits class B and class C. Now the question regarding method of which class would be resolved first arises when the called method in the instance of class D is not available in class D itself and the interpreter has to climb one-level higher where both the parent classes of D,i.e. class B and class C are available. So, let us remove this method from class D and see what does interpreter do.

Click here to Copy the Code

Here’s the output of the above program:

Even though, B and C both had the required method, interpreter called the method of class B and not the method from class C. Now, what if even class B does not have the called method ?

Before taking the example and seeing what do we get as the output, we have to also note that class B and class C also inherits the class A. Thus. class A is the base class or parent class of class B and class C. Moreover, class D is a subclass or child class of class B and class C. Following figure, which is similar to a diamond, represents our hierarchy of classes. Due to this resemblance to the shape of diamond, and problem arising regarding the resolution order of methods, this situation is also known as diamond problem. Similar situation also arises with multiple-inheritance in C++ also.

diamond Problem

Let’s get back to resolution order of methods and remove the required method from class B as given in the code below.

Click here to Copy the Code

Here’s the output of the above program:

It can be seen from output that, although class C had the called method, interpreter called the required method from class A. This is because class A is the parent of class B and interpreter always searches the called method with the parent class if it is not found in the child class. Thus, instead of searching the called method at the same level, interpreter always goes one-level above the current level of the child class. As we have taken, few examples already, let us take one more example where we remove the required method even from class A.

Click here to Copy the Code

Here’s the output of the above program:

Now, as the required method was not available in A in went lowered down a level and tried searching the method over there and is not was in B, it called the method from C. Thus, we can say that following is the method resolution order:

D -> B -> A -> C

Hence, we can say that the methods in multiple-inheritance are resolved used depth-first left-to-right order.

Just for curious minds let us take one more example with the code given below:

Click here to Copy the Code

Now as per our knowledge of method resolution order which we just understood in the examples given above. The output of this code should be the method being called from class A. So, let’s confirm it and check the following output:

Oh! we got the output as the method of class C instead of A. Has our interpreter gone nuts? Have we made any mistake? Nope, we have done everything perfectly and even the interpreter works 100% right. So, where’s multiple inheritance then? It’s gone, yes it has gone but how?

You can see in our last example, class A is the modern python class as it inherits the object class and thus there is no multiple inheritance. As our class A, already has a base class, it cannot become base class of any other class and thus there is no possibility of multiple inheritance. Although, as per the concepts, we can see the multi-level inheritance running perfectly as it should. This is the reason that Python 3 does not support multiple inheritance while it does supports multi-level inheritance.

That’s all folks for this post. Next time, there will be a post on important sorting algorithms in Python Programming by my friend Kaushik Vaghani while I will be back with Regular Expressions in Python soon after that.