In this tutorial, you’ll find out about multiple inheritances in Python and how to use it in your program. You’ll also find out about multi-level inheritance and method resolution order.
Python Multiple Inheritance
A class can be derived from more than one base class in Python, like C++. This is called multiple inheritances.
In multiple inheritances, the highlights of all the base classes are acquired into the determined class. The grammar for multiple inheritances is similar to single inheritance.
class Base1: pass class Base2: pass class MultiDerived(Base1, Base2): pass
Here, the MultiDerived class is derived from Base1 and Base2 classes.
The MultiDerived class inherits from both Base1 and Base2 classes.
Python Multilevel Inheritance
We can also inherit from a determined class. This is called multilevel inheritance. It can be of any profundity in Python.
In multilevel inheritance, features of the base class and the determined class are acquired into the new inferred class.
An example of the corresponding visualization is given below.
class Base: pass class Derived1(Base): pass class Derived2(Derived1): pass
Here, the Derived1 class is derived from the Base class, and the Derived2 class is derived from the Derived1 class.
Method Resolution Order in Python
Every class in Python is derived from the object class. It is the most base type in Python.
So technically, all other classes, either built-in or user-defined, are derived classes and all objects are instances of the object class.
# Output: True print(issubclass(list,object)) # Output: True print(isinstance(5.5,object)) # Output: True print(isinstance("Hello",object))
In the multiple inheritance scenario, any predefined property is looked through first in the current class. If not found, the pursuit proceeds into parent classes inside and out first, left-right design without looking through a similar class twice.
So, in the above example of MultiDerived class, the search order is [MultiDerived, Base1, Base2, object]. This order is also called linearization of MultiDerived class and the set of rules used to find this order is called Method Resolution Order (MRO).
MRO must prevent local precedence ordering and also provide monotonicity. It ensures that a class always appears before its parents. In the case of multiple parents, the order is the same as tuples of base classes.
MRO of a class can be viewed as the __mro__ attribute or the MRO() method. The former returns a tuple while the latter returns a list.
>>> MultiDerived.__mro__ (<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>) >>> MultiDerived.mro() [<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>]
Here is a little more complex multiple inheritance example and its visualization along with the MRO.
# Demonstration of MRO 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 # Output: # [<class '__main__.M'>, <class '__main__.B'>, # <class '__main__.A'>, <class '__main__.X'>, # <class '__main__.Y'>, <class '__main__.Z'>, # <class 'object'>] print(M.mro())
[<class '__main__.M'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.Z'>, <class 'object'>]
Please feel free to give your comment if you face any difficulty here.
For more Articles click on the below link.