Python Multiple Inheritance



Python Multiple Inheritance

Like in C++, in Python, a class can be derived from more than one class. This is called multiple inheritance.

When using multiple inheritance, the features of all parent (base) classes are inherited into the child (derived) class.


Python Multiple Inheritance Syntax

The syntax for multiple inheritance can be given as follows:

class Base1:
    pass

class Base2:
    pass

class Derived(Base1, Base2):
    pass

As we can see above, the Derived class inherits from Base1 and Base2 classes.

multiple inheritance in Python

The Derived class is derived from both Base1 and Base2 classes.


Python Multilevel Inheritance

Multilevel inheritance can be done by inheriting from a derived class. It can be of any depth in Python.

When using multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class.

Let us see an example of multilevel inheritance.

class GrandFather:
    pass

class Father(GrandFather):
    pass

class Son(Father):
    pass

Here, the Father class is derived from the GrandFather class, and the Son class is derived from the Father class.

The following illustration gives a more detailed picture of multilevel inheritance.

multilevel inheritance in Python

Method Resolution Order in Python

In Python, every class is a subclass from the object class. So all classes, either built-in or user-defined, are derived classes, and all objects are instances of the object class.

print(issubclass(int, object))

print(isinstance("hello world", object))

print(isinstance(33, object))

Output

True
True
True

Method Resolution Order (MRO) is the way a programming language resolves a method or attribute.

In the multiple inheritance scenario, the method resolution order consists of searching for any specified attribute first in the current class. If not found, the search continues into parent classes in depth-first, left-right without searching the same class twice.

So, in the above example of the Derived class the search order is [Derived, Base1, Base2, object]. This order is known as a linearization of the Derived class.

MRO prevents local priority ordering and provides monotonicity. It ensures that a class always appears before its parents. If a class inherits from multiple classes, they are put in the same order specified in the tuple of the base class.

To view the MRO of a class, we can use the __mro__ or the mro() method. The first one returns a tuple, while the second returns a list.

>>> Derived.__mro__

(<class '__main__.Derived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>)

>>> Derived.mro()

[<class '__main__.Derived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>]

We will see a more complex multiple inheritance example and its illustration with the MRO in the following example.

complex multiple inheritance in Python
class A:
    pass

class B:
    pass

class C:
    pass

class M(A, B):
    pass

class N(B, C):
    pass

class X(M, N, A):
    pass

print(M.mro())

Output

[<class '__main__.M'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.