Python's many inheritance and the use of __mro__


1
class Base(object): 2 def text(self): 3 print('------text-----') 4 class A(Base): 5 def text(self): 6 print('------text1-----') 7 class B(Base): 8 def text(self): 9 print('------text2-----') 10 class C(A,B): 11 def text(self): 12 print('------text3-----') 13 14 c = C() 15 c.text()

Multiple inheritance: The method of use is similar to single inheritance, which conceptually means that a subclass contains multiple parent classes.

In the above code, when we call the method in the C class, since all the parent classes it inherits have the same method name, we can print print(class name.__mro__) to view the specifics of the system call method. What is the order.

print(C.__mro__)

The displayed results are as follows:

Indicates that the calling priority is: C>A>B>Base>object

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296913&siteId=291194637