MRO,C3算法

时间:2024-08-09 18:03:50
 1了解python2和python3类的区别
python2在2.4之前使用的是经典类, 2.4之后, 使用的是新式类 class Foo:
pass class Foo(object):
pass MRO: method resolution order 方法的查找顺序 class Base:
pass class Base1:
def chi():
pass class Bar(Base, Base1):
pass b = Bar() # Bar -> Base -> Base1
b.chi() 2. 经典类的MRO 树形结构的深度优先遍历 -> 树形结构遍历 3. 新式类的MRO C3算法(难点, 重点)
1. 拆分
2. 合并 用头和身体比较
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
class E(C, A):
pass
class F(D, E):
pass
class M:
pass
class N(M):
pass
class P(E, A):
pass
class X:
pass
class Q(P,N,X):
pass
class G(Q, F):
pass
class H(G, F):
pass # L(A) = A
# L(B) = B + L(A) + A
# L(C) = C + L(A) + A
# L(D) = D + L(B) + L(C) + BC
# L(E) = E + L(C) + L(A) + CA
# L(F) = F + L(D) + L(E) + DE
# L(M) = M
# L(N) = N + L(M) + M
# L(P) = P + L(E) + L(A) + EA
# L(X) = X
# L(Q) = Q + L(P) + L(N) + L(X) +PNX
# L(G) = G + L(Q) + L(F) + QF
# L(H) = H + L(G) + L(F) + GF
#
#
# L(A) = A
# L(B) = B + L(A) + A # BA
# L(C) = C + L(A) + A # CA
# L(D) = D + L(B) + L(C) + BC # DBCA
# L(E) = E + L(C) + L(A) + CA # ECA
# L(F) = F + L(D) + L(E) + DE # FDBECA
# L(M) = M
# L(N) = N + L(M) + M # NM
# L(P) = P + L(E) + L(A) + EA # PECA
# L(X) = X
# L(Q) = Q + L(P) + L(N) + L(X) +PNX # QPECANMX
# L(G) = G + L(Q) + L(F) + QF # GQPFDBECANMX
# L(H) = H + L(G) + L(F) + GF # HGQPFDBECANMX