Reprint python multiple inheritance C3 algorithm

Source: Internet
Author: User

Remark: O==object

2.PYTHON-C3 Algorithm Analysis:

#C3 definition Reference starts

C3 algorithm: MRO is an ordered list l, which is calculated 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 of at least one element in the list that is the class itself.

For example:

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) # first list do header D, other lists such as EO footer do not contain d, so you can put D, that is, D is the legal header

= B + D + merge (O, EO) #从第一个开始表头是O, but the later list EO's footer contains o so O is not legal, 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是合法表头

= A + B + merge (deo,cefo,c) #D是合法表头

= A + B + D + merge (eo,cefo,c) #E不是合法表头, jumps to the next list Cefo, at which point C is the legal header

= A + B + D + C + merge (EO,EFO) #由于第三个列表中的C被删除, empty, so there is no third table, only two tables are left, and E is the legal header

= A + B + D + C + E + merge (O,FO) #O不是合法表头, jump to the next list fo,f is a legitimate header,

= A + B + D + C + E + F + merge (o,o) #O是合法表头

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

= [A,b,d,c,e,f,o]

Reprint python multiple inheritance C3 algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.