Why the base class doesn't work in this multiple inheritance?
我对解释以下代码的MRO和C3线性化算法感到困惑,请你帮我一下好吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class A(object): def go(self): print("A") class B(A): def go(self): super(B, self).go() print("B") class C(A): def go(self): # super(C, self).go() print("C") class D(B, C): def go(self): super(D, self).go() print("D") d = D() d.go() pprint.pprint(D.__mro__) The Result is: C B D (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>) |
另一个例子是:如果我取消类C中的super语句的注释,并在类B中注释super语句,结果将是"b d",没有c和a。为什么?!!!!!!!(我确实先了解深度,从左到右规则,但是……)
如果希望调用所有
你可以从guido自己的博客上找到更多关于mro和
从圭多的博客:
The computation of the MRO was officially documented as using a depth-first left-to-right traversal of the classes as before. If any class was duplicated in this search, all but the last occurrence would be deleted from the MRO list.
号
例如,如果你有:
1 2 3 4 | class A: pass class B(A): pass class C(A): pass class D(B, C): pass |
MRO不使用
1 | D.go() --> B.go() (--> A.go() removed by MRO) --> C.go() (x> A.go() not called) |
号