The MRO and multiple inheritance __python in Python

Source: Internet
Author: User

Related concepts:

Mro:method resolution order, the analytic sequence of methods, is the algorithm used in Python to deal with two semantic problems.

Ambiguity

Python supports multiple inheritance, and multiple-inheritance languages tend to encounter the following two classes of two-semantic problems: two base classes A and B,a and B both define method F (), C inherits A and B, and the F () method of C is called undefined. There is a base class A, defined Method F (), Class B and Class C inherit Class A (f () method), Class D inherits B and C, and a problem arises, D does not know whether to inherit B's F () method or C's F () method.

C + + is also one of the languages that support multiple inheritance. In problem 1,c++, the subclass methods and properties are invoked as precedence, and if you want to access the masked base class members in a subclass, you should qualify (Baseclassname::func ()) with the base class name. To solve the problem 2,c++ by virtual inheritance, the virtual keyword is used to modify the common direct base class, thus guaranteeing that no multiple base class replicas will produce ambiguity.

In Python, the C3 algorithm avoids the above two categories of two semantics.

The depth-first algorithm (Dfs,depth-first-search) pushes the root node into the stack. Every time you pop an element from the stack, search for all the elements that are next to it, and push those elements into the stack. and write this element as a precursor to its next level element. End the program when you find the element you are looking for. If traversing the entire tree has not been found, end the program.

The breadth-first algorithm (Bfs,breadth-first-search) places the root node at the end of the queue. Each time you remove an element from the head of the queue, look at the next level of elements of the element and place them at the end of the queue. and write this element as a precursor to its next level element. End the program when you find the element you are looking for. If traversing the entire tree has not been found, end the program.

Topology Sort:

A topological sort of a directed graph (acyclic graph abbreviation dag) G is performed. is to arrange all vertices in G into a linear sequence, so that any pair of vertices u and V, if Edge (u,v) ∈e (g), then u appear in the linear sequence before v. Typically, such a linear sequence is called a sequence that satisfies the topological ordering (Topologicalorder), referred to as a topological sequence.

Implementation steps for topology sorting:

The loop performs the following two steps until the vertex with the entry of 0 is selected and the vertex is exported, and the vertex and all the edges are deleted from the network.

Two ways to invoke the parent class method in Python:

Class A (object):

def __init__ (self):

Self.name = "A:name"

Print "a:__init__"

def fun (self):

Print "A:fun"

Class B (A):

def __init__ (self):

Print "b:__init__"

A.__init__ (self) # calls directly with the class name

Super (B, self). __init__ () # called with the Super keyword

def fun (self):

Print "B:fun"

A.fun (self)

Super (B, self). Fun ()

Print Self.name

There is no intrinsic difference between these two approaches to single inheritance, but when multiple inheritance occurs, the base class of super is not necessarily the base class we think, and we look at the following example:

Class A (object):

def __init__ (self):

Print "Enter A"

Print "Leave A"

Class B (object):

def __init__ (self):

Print "Enter B"

Print "Leave B"

Class C (A):

def __init__ (self):

Print "Enter C"

Super (C, self). __init__ ()

Print "Leave C"

Class D (A):

def __init__ (self):

Print "Enter D"

Super (D, self). __init__ ()

Print "Leave D"

Class E (B, C):

def __init__ (self):

Print "Enter E"

B.__init__ (self)

C.__init__ (self)

Print "Leave E"

Class F (E, D):

def __init__ (self):

Print "Enter F"

E.__init__ (self)

D.__init__ (self)

Print "Leave F"

f = f ()

Output results:

Enter F

Enter E

Enter B

Leave B

Enter C

Enter D

Enter A

Leave A

Leave D

Leave C

Leave E

Enter D

Enter A

Leave A

Leave D

Leave F

The inheritance relationship of the class is as follows:

Object

| \

| A

| / |

B C D

\   /   |

E |

\   |

F

The idea is that when we call the constructor, the constructor for the base class is also invoked, but the actual results show that the constructors for a and d are called 2 times, and strangely, when you call Super (C, self). __init__ (), you actually enter the constructor of D. This is also why D's constructor was called two times (one for F and one for C). From an inheritance relationship, the base class for C should be

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.