English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Experience Notes

Online Tools

Python Functions

Python Data Types

Python Basic Tutorial

Python File I

O

Python Objects and Classes

Python Date and Time

Python Multiple Inheritance

Advanced Knowledge of Python

Python Reference Manual

In this article, you will learn what multiple inheritance is in Python and how to use it in programs. You will also learn about multiple inheritance and method resolution order. ++multiple inheritance in Pythonlike Cclass

In multiple inheritance, all the functions of all the base classes are inherited into the derived class. The syntax of multiple inheritance is similar to that of a singleinheritance.

Example

class Base1:
    pass
class Base2:
    pass
class MultiDerived(Base1, Base2)
    pass

Here,MultiDerivedfrom the classBase1andBase2.

The MultiDerived class inherits from Base1and Base2inheritance.

Multiple inheritance in Python

On the other hand, we can also inherit the derived class. This is called multiple inheritance. In Python, it can be of any depth.

In multiple inheritance, the functions of the base class and derived class are inherited into the new derived class.

The following example is given with corresponding visual effects.

class Base:
    pass
class Derived1(Base):
    pass
class Derived2(Derived1)
    pass

Here,Derived1fromBasederived,Derived2fromDerived1Derived.

Method resolution order in Python

Each class in Python derives from this class object. It is the most basic type in Python.

Therefore, technically, all other classes (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 a multiple inheritance scheme, any specified attribute will be searched first in the current class. If not found, the search will continue in a depth-first, left-to-right manner into the parent classes without searching the same class twice.

So, in the above example, the search order of the MultiDerived class is [MultiDerived, Base]1, Base2, object]. This order is also known as the linearization of the MultiDerived class, and the set of rules used to find this order is called " Method resolution order (MRO).

MRO must prevent local priority sorting and must also provide monotonicity. It can ensure that a class always appears before its parent class, and if there are multiple parent classes, its order is the same as the tuple of the base class.

The MRO of a class can be considered as the __mro__ attribute or 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'>]

This is a slightly more complex multiple inheritance example, its visualization, and 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())

Please refer to this content to furtherDiscuss MRO,and understand the actual calculation method of the algorithm.