1. 程式人生 > >mro c3演算法

mro c3演算法

 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. 合併

用頭和身體比較
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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

  

   
 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. 合併

用頭和身體比較
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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