[英]Python super() in nested multiple-inheritance
I recently had a problem understanding the code below: 我最近在理解以下代码时遇到了问题:
class A(object):
def __init__(self):
print("go A go!")
class B(A):
def __init__(self):
super(B, self).__init__()
print("go B go!")
class C(A):
def __init__(self):
super(C, self).__init__()
print("go C go!")
class D(B,C):
def __init__(self):
super(D, self).__init__()
print("go D go!")
d = D()
The result I got is: 我得到的结果是:
go A go!
go C go!
go B go!
go D go!
I understand the use of super() in simple multiple-inheritance but I really don't know how it got this result. 我了解在简单的多重继承中使用super(),但我真的不知道它是如何得到此结果的。
Python uses C3 linearlization to define the order (Method Resolution Order ). Python使用C3线性化来定义顺序(方法解析顺序)。 It produced the following order:
它产生了以下命令:
D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, builtins.object)
Since you are calling the super first and the printing, it results the printing in reverse order (similar to call order in recursion) 由于您要先调用超级并打印,因此打印结果的顺序相反(类似于递归的调用顺序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.