Python multiple inheritance New algorithm C3 introduction _python

Source: Internet
Author: User
Tags inheritance in python

The MRO method is the resolution order, which is mainly used to determine the path (from which class) of the property on multiple inheritance.

In the python2.2 version, the basic idea of the algorithm is to compile a list, including the searched class, based on the inheritance structure of each ancestor class, to delete duplicates by policy. However, in the maintenance of monotonicity has failed (sequential save), so from the 2.3 version, the use of a new algorithm C3.

Why use the C3 algorithm

The C3 algorithm was first proposed for Lisp, and it was used in Python to solve the problem that the original depth-first search algorithm did not satisfy the local priority and monotonicity.

Local priority: The order in which the parent class is declared, such as C (A,B), and if you are accessing the properties of Class C objects, you should prioritize the class A by the order of declaration, and then look for class B.

Monotonicity: If in the parsing order of C, a is in front of B, then in all subclasses of C, this order must also be met.

C3 algorithm

To judge the MRO first determine a linear sequence, and then find the path determined by the order of the classes in the sequence. So the C3 algorithm is to generate a linear sequence.

If you inherit to a base class:

Copy Code code as follows:

Class B (A)

Then B's MRO sequence is [B,a]

If you inherit to more than one base class

Copy Code code as follows:

Class B (A1,a2,a3 ...)

Then B's MRO sequence MRO (b) = [B] + merge (MRO (A1), MRO (A2), MRO (A3) ..., [A1,A2,A3])
The merge operation is the core of the C3 algorithm.

Traverses the sequence that executes the merge operation, and if the first element of a sequence is the first element in another sequence, or does not appear in another sequence, the element is removed from all the sequence of execute merge operations and merged into the current MRO.

Sequence after the merge operation, continue the merge operation until the sequence of the merge operation is empty.

If the sequence of the merge operation cannot be empty, it is illegal.

Example:

Copy Code code as follows:

Class A (O):p
Class B (O):p
Class C (O):p
Class E (a,b):p
Class F (b,c):p
Class G (e,f):p

A, B, and C are inherited to a base class, so the MRO sequence is [A,o], [B,o], [C,o]

Copy Code code as follows:

MRO (e) = [E] + merge (MRO (A), MRO (B), [a,b])
= [E] + merge ([A,o], [B,o], [a,b])

The sequence for performing the merge operation is [A,o], [B,o], [a,b]

A is the first element in the sequence [A,o], does not appear in the sequence [B,o] and is the first element in the sequence [a,b], delete A from the sequence performing the merge operation ([A,o], [B,o], [a,b]), and merge into the current mro,[e].
MRO (E) = [E,a] + merge ([O], [B,o], [B])

The merge operation is performed, O is the first element in the sequence [o], but o appears in the sequence [B,o] and is not the first element. Continue to view the first element of [B,o] b,b satisfy the condition, so remove B from the sequence in which the merge operation is performed, and merge it into [E, A].

Copy Code code as follows:

MRO (E) = [E,a,b] + merge ([o], [o])
= [E,a,b,o]

Code to implement the C3 algorithm

Copy Code code as follows:

#-*-ENCODING:GBK-*-#
def mro_c3 (*CLS):
If Len (CLS) ==1:
If not cls[0].__bases__:
Return CLS
Else
Return cls+ mro_c3 (*cls[0].__bases__)
Else
SEQS = [List (MRO_C3 (c)) for C in CLS] +[list (CLS)]
res = []
While True:
Non_empty = List (filter (None, seqs))
If not non_empty:
Return tuple (RES)
For the SEQ in Non_empty:
candidate = Seq[0]
Not_head = [s for s in Non_empty if candidate in S[1:]]
If Not_head:
candidate = None
Else
Break
If not candidate:
Raise TypeError ("Inconsistent hierarchy, no C3 MRO is possible")
Res.append (Candidate)
For the SEQ in Non_empty:
If seq[0] = = Candidate:
Del Seq[0]

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.