Reproduced python multiple inheritance C3 algorithm

Remarks: O==object

2.Analysis of python-C3 algorithm:

#C3 Define the start of the reference

C3 Algorithm: The MRO is an ordered list L that is computed when the class is created.

L(Child(Base1,Base2)) = [ Child + merge( L(Base1) ,  L(Base2) ,  Base1Base2 )]

L(object) = [ object ]

Properties of L: The result is a list with at least one element in the list, the class itself.

E.g:

L(D) = L(D(O))

     = D + merge (L (O))

     = D + O

     = [D,O]

L(B) = L(B(D,E))

     = B + merge (L (D), L (E))

     = B + merge(DO , EO) # The header D of the first list DO, other lists such as EO do not contain D at the end, so D can be brought up, that is, D is a legal header

     = B + D + merge(O , EO) #The header is O from the first one, but the tail of the following list EO contains O, so O is illegal, so skip to the next list EO

     = B + D + E + merge (O, O)

     = [B,D,E,O]

Similarly:

L(C) = [C,E,F,O]

L(A(B,C)) = A + merge(L(B),L(C),BC)

          = A + merge(BDEO,CEFO,BC)#B is a valid header

          = A + B + merge(DEO,CEFO,C)#D is a valid header

          = A + B + D + merge(EO,CEFO,C)#E is not a valid header, skip to the next list CEFO, at this time C is a valid header

          = A + B + D + C + merge(EO,EFO)#Because C in the third list is deleted and empty, there is no third table, only two tables are left; E is legal at this time header

          = A + B + D + C + E + merge(O,FO)#O is not a valid header, skip to the next list FO, F is a valid header,

          = A + B + D + C + E + F + merge(O,O)#O is a valid header

          = A + B + D + C + E + F + O

          = [A,B,D,C,E,F,O]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324977426&siteId=291194637