新式类(广度优先)
image.pngclass A(object):
title = 'A'
pass
class B(A):
# title = 'B'
pass
class C(A):
# title = 'C'
pass
class D(A):
# title = 'D'
pass
class E(B, C):
# title = 'E'
pass
class F(C, D):
# title = 'F'
pass
class H(E, F):
# title = 'H'
pass
print(H.title)
# H E B F C D A
经典类(深度优先)
image.pngclass A:
# title = 'A'
pass
class B(A):
# title = 'B'
pass
class C(A):
# title = 'C'
pass
class D(B, C):
# title = 'D'
pass
class E(D):
# title = 'E'
pass
class F(D):
title = 'F'
pass
class H(E, F):
pass
print(H.title)
# E D B A C F
image.png
首先找入度为0的点,只有一个A,把A拿出来,把A相关的边剪掉,再找下一个入度为0的点,有两个点(B,C),取最左原则,拿B,这是排序是AB,然后剪B相关的边,这时候入度为0的点有E和C,取最左。这时候排序为ABE,接着剪E相关的边,这时只有一个点入度为0,那就是C,取C,顺序为ABEC。剪C的边得到两个入度为0的点(DF),取最左D,顺序为ABECD,然后剪D相关的边,那么下一个入度为0的就是F,然后是object。那么最后的排序就为ABECDFobject。
A.__mro__的结果
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>, <type 'object'>)
网友评论