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

Python Basic Tutorial

Python Flow Control

Python Funktion

Python Datentypen

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python Mehrfachvererbung

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

Multiple inheritance in Python

like C ++like aclassIt can be derived from multiple base classes in Python. This is called multiple inheritance.

In multiple inheritance, all the functions of all 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 derives from Base1and Base2inheritance.

Multiple inheritance in Python

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

In multiple inheritance, the functions of the base class and the 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.

Reihenfolge der Methodenauflösung in Python

Every 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 the case of multiple inheritance, any specified attribute is first searched within the current class. If not found, the search continues in a depth-first, left-to-right manner into the parent classes without searching the same class twice.

Also, in the above example, the search order in the MultiDerived class is [MultiDerived, Base1,Base2,object]。Diese Reihenfolge wird auch als Linearisierung der Klasse MultiDerived bezeichnet und die Regelset, das diese Reihenfolge findet, wird als " Methodenauflösungsreihenfolge (MRO).

MRO muss lokale Prioritätenreihenfolgen verhindern und auch Monotonie bieten. Es kann sicherstellen, dass eine Klasse immer vor ihren Elternklasse erscheint und wenn es mehrere Elternklassen gibt, ist die Reihenfolge der gleichen wie im Tupel der Basisklasse.

Man kann die MRO der Klasse als __mro__-Attribut oder mro()-Methode betrachten. Der Erste gibt ein Tupel zurück, während der Letzte eine Liste zurückgibt.

>>> 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'>]

Dies ist ein etwas komplexerer Beispiel mit mehrfacher Vererbung sowie seiner Visualisierung und 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
# Ausgabe:
# [<class '__main__.M'>, <class '__main__.B'>,
# <class '__main__.A'>, <class '__main__.X'>,
# <class '__main__.Y'>, <class '__main__.Z'>,
# <class 'object'>]
print(M.mro())

Bitte beziehen Sie sich auf diesen Inhalt, um weiterDiskutieren Sie MROund verstehen Sie die Berechnungsmethoden der tatsächlichen Algorithmen.