In Python3, all classes are new classes (the default inherits obj, with the Super,mro method), with breadth first, That is, the topological sorting algorithm in python2.7, the new class and the classic class coexist, the classical class uses the depth first algorithm, namely the longitudinal super Method Essence, is not simply looking for the parent class, but is according to the caller's node position to carry on the breadth precedence order, what is the topological sort
In graph theory, topological ordering (topological sorting) is a linear sequence of all the vertices of a forward-free graph (dag,directed acyclic graph) . And the sequence must meet the following two conditions:
- Each vertex appears and appears only once.
- If there is a path from vertex A to vertex B, vertex a appears in front of Vertex b in the sequence.
For example, the following figure:
It's a DAG diagram, so how do you write out its topological order? Here's a more common approach:
- Select a vertex without a precursor (that is, 0) on the way to the DAG and output
- Deletes the vertex and all the forward edges that start with it.
- Repeat 1 and 2 until the current DAG diagram is empty or there is no precursor vertex on the current route. The latter case indicates that there must be a ring in the graph.
So, the result of getting the topological sort is {1,2,4,3,5}
To get a better understanding of how this topology is sorted, let's take a look at what the sort results are, and whether it inherits what you want.
#!/usr/bin/env Python3#-*-Coding:utf-8-*-ClassA(Object):DefFoo(Self):Print(' A foo ')DefBar(Self):Print(' A Bar ')ClassB(Object):DefFoo(Self):Print(' B foo ')DefBar(Self):Print(' B Bar ')ClassC1(A):PassClassC2(B):DefBar (selfprint ( ' C2-bar ' ) class d (c1 ,c2): passif __ name__ == ' __main__ ' : print (d. __mro__) d=d () d foo () d. Bar ()
Or do you first construct an inheritance map based on the inheritance relationship?
- Find the vertex with a degree of 0, only one D, take D, cut off D-related edges
- Get two degrees to 0 vertex (C1,C2), according to the leftmost principle, take C1, cut off C1 related side, this time the sequence is {D,C1}
- Next, the vertex with a degree of 0 has two (A,C1), according to the leftmost principle, take a, cut off a related side, this time the sequence is {d,c1,a}
- Then see, the vertex of the degree of 0 is C2, take C2, cut off C2 related side, this time the sequence is {D,C1,A,C2}
- To continue, the vertex with a degree of 0 is B, take B, cut off the related side of B, and finally an object
- So the last sequence is {d,c1,a,c2,b,object}
Finally, we execute the above code and find print(D.__mro__) the result as calculated above
Finally, the Python inheritance sequence follows the C3 algorithm, and as long as the desired content is found in one place, the search is no longer continued
Topological ordering of Python multiple inheritance