【发布时间】:2017-12-30 03:33:14
【问题描述】:
class Z():
def func():
print ("I'm in Z")
class A(Z):
def func():
print ("I'm in A")
class B(Z):
def func():
print ("I'm in B")
class C(A,B):
pass
#def func():
# print ("I'm in C")
ob1 = C
print ("Calling function func ")
ob1.func()
输出是:
Calling function func
I'm in A
但是根据How does Python's super() work with multiple inheritance?
顺序应该是“深度优先从左到右遍历”+“删除重复期望最后一个”
正如 /visionscaper(用户:889617)所解释的,
为什么会有这种差异?
【问题讨论】:
-
它的深度从左到右。作为 C 的父母,A 离开了 B。是什么绊倒了你?
-
从C开始,深度优先从左到右遍历的下一个基类是C的最左边基类:A。你期待什么?
-
此处的查找顺序为 A、Z、B(A、Z 从 B 层次结构中删除为重复项。)
-
我将继续投票结束,因为不清楚。
-
@MadPhysicist:您的订单不正确。在 MRO 中,
B必须出现在Z之前。
标签: python method-resolution-order