In this tutorial, we’ll learn how to easily use Python multiple inheritance. We’ll be uncovering different methods on how to achieve Python multi inheritance. For detailed demonstration, we’ll go through practical Python code examples.
Introduction: Python Multiple Inheritance
In our previous article, we’ve discussed about simple Python inheritance. Click here if you want to learn it.
Multiple inheritance in Python means that a child class can have more than one parent class. By this method, the child/derived class can possess attributes/methods of all these parent classes.
Child class can also inherit from another child/derived class. We’ll now practically discuss these concepts using proper code examples.
Implementing Python Multiple Inheritance
In order to understand Python multiple inheritance, we can use two methods. See below:
- Python Multi Level Inheritance
- Python Method Resolution Order
Python Multi Level Inheritance
class A: print('Parent Class') class B(A): print('Can access features of class A') class C(B): print('Can access features of class A and Class B')
This is how multi level Python inheritance works. Class C object can access features/attributes of both class B and class A.
Let’s now understand it with a practical code example.
Example: Simple Python Multi Level Inheritance
class A: def addFun(self,a,b): print(a+b) class B(A): def subFun(self,a,b): print(a-b) class C(B): def divFun(self,a,b): print(a/b) obj=C() obj.divFun(3,4) obj.subFun(5,2) obj.addFun(2,8)
0.75 3 10
We’ve created three classes. Class A is a base class. Class B is a derived class of A while class C is a derived class of B. We can see that by only creating the object of class C, we can easily use/call the methods of both the parent and derived classes.
But we’ve to understand that the class object can only use the methods/attributes of classes above it (parent classes). Let’s use the above example and try to create object of class B. After that, we’ll try to call the function of class C which is a child class to class B. See below code:
obj=B() obj.divFun(3,4) obj.subFun(5,2) obj.addFun(2,8)
3 10 AttributeError: 'B' object has no attribute 'divFun'
We can see that object of class B has successfully fetched the methods of its own class and its parent class (class A). But when it comes to using the method of its child class, then its throws an error.
So by this, we can understand that a derived class can only reuse attributes of its own class or its parent class.
Python Method Resolution Order
class A: class B: class C: class D(C,A,B):
This is how this method can be specified. We’ve 3 classes and all are parent to class D. The order is very important.
For instance, we’ve a method of name fun in all of these classes and we want to call it using the object of class D. Then first, it’ll check for that method in its own class. If not found, then it’ll go to its parent classes in the order defined. If C is first, then it’ll go to C and search for that method. If not found, then it’ll go to class A and then B. In our case, this is the order we’ve defined. You can use any order you want.
Example: Each Class Having Method Of Same Name
Let’s now convert the above theory into a practical Python code example.
class A: def fun(self): print('This is Class A') class B: def fun(self): print('This is Class B') class C: def fun(self): print('This is Class C') class D(B,C,A): def fun(self): print('This is Class D') obj=D() obj.fun()
This is Class D
As we can see, the same method was first searched in its own class and found successfully. So it is called. As a result, the print function of this method is executed.
Let’s change the name of method in class D and then see the result.
Same above code is used but in this case, the name of method is changed in class D.
class D(B,C,A): def newFun(self): print('This is Class D') obj=D() obj.fun()
This is Class B
As we can see that first it checked for the method in its own class. Its not found so it has checked in class B as this class comes first in the order. If the method was not found here also then class C would be checked and then class A. This is how Python method resolution order works.
So this is how we can use Python multiple inheritance. Do share it with your developer friends if you like this post. Also, don’t hesitate to ask if you still have any questions regarding Python multiple inheritance.
To conclude this tutorial, hope you now have an in-depth practical knowledge of Python multiple inheritance. We’ll be glad to receive your feedback on this article.
Do visit our other articles on Python programming. Thank you for reading this post.