Python 2.6 打印 Root 因为Root
是一个老式的类。它应该打印B
if Root
继承自object
(使其成为新式课程)。
旧式类不使用 C3 MRO,而是使用旧的有缺陷的 MRO,这正是您引用的文本所描述的。
B之所以出现在Root之前,是因为B也继承自Root。
AB的继承图为:
AB - A - Root -- object
\ /
B
So:
mro(object) = [object]
mro(Root) = [Root] + mro(object) # mro of single-inherited class is simple...
= [Root, object]
mro(A) = [A, Root, object]
mro(B) = [B, Root, object]
在多重继承的情况下,MRO 是通过从左到右获取超类 MRO 中的元素来计算的,不会出现在中间。在示例中更好地解释:
mro(AB) = [AB] + merge(mro(A), mro(B))
= [AB] + merge([A, Root, object], [B, Root, object])
= [AB] + [A] + merge([Root, object], [B, Root, object])
# A appears at head, pop it.
= [AB] + [A] + [B] + merge([Root, object], [Root, object])
# cannot pop Root because Root is not at head of [B, Root, object]
# pop B instead.
= [AB] + [A] + [B] + [Root] + merge([object], [object])
# now we can pop Root because it only appears at the head position
= [AB] + [A] + [B] + [Root] + [object]
= [AB, A, B, Root, object]
更多详细信息可以在http://www.python.org/download/releases/2.3/mro/ http://www.python.org/download/releases/2.3/mro/.