Python Super Inheritance

Source: Internet
Author: User

The MRO (Method resolution Order) is used by Python to parse the order of the method calls, and the MRO records the class type sequence of all the base classes of a class, and super does not simply call the method of the base class, but instead invokes the method of the class in the order of the MRO.
When super () is used, it should be used in all classes, otherwise there may be cases where the class constructor is not called.
#!/usr/bin/python
#-*-Coding:utf-8-*-
Class A (object):
def __init__ (self):
print ' A __init__ '
Super (A, self). __init__ ()
print ' Leave A '
Class C (object):
def __init__ (self):
print ' C __init__ '
Super (C, self). __init__ ()
print ' Leave C '
Class B (A,C):
def __init__ (self):
print ' B __init__ '
Super (B, self). __init__ ()
print ' Leave B '
Class D (B):
def __init__ (self):
print ' D __init__ '
Super (D, self). __init__ ()
print ' Leave D '
if __name__ = = ' __main__ ':
D ()

The output is:
D __init__
B __init__
A __init__
C __init__
Leave C
Leave A
Leave B
Leave D

The compilation process: Super (D,self) finds the parent class of D and finds the parent of super (class name, self) according to the breadth-first algorithm .

By the way, what is the difference between a classic class and a new class?

(1) in python2.x, a class that inherits from object is called a new class (such as Class A (object)) that does not inherit from object is called a classic class (such as Class A ()).
(2) The new class object can get its own type directly from the __class__ property: Type
(3) The Order of inheritance search has changed, the classic class multi-inheritance when the property search order: First deep inheritance tree left, and then return, start looking for the right side ( that is, depth first search ); New class multi-inheritance Property search Order: Horizontal Search first, then move up ( that is, breadth-first search ).

As shown in the following:

MRO is method resolution order, which is used primarily to determine the path (from which class) of a property that is tuned at multiple inheritance.
Before looking at a lot of data, MRO is based on the depth-first search algorithm. But not exactly right before Python2.3 was based on this algorithm, but a new algorithm was applied from Python2.3: C3 algorithm. Why use the C3 algorithmThe C3 algorithm was first proposed to be used in Lisp, and it was applied in Python to solve the problem that the original depth-first search algorithm did not satisfy the local priority and monotonicity. Local precedence : refers to the order of the parent class at the time of declaration, such as C (A, A, b), and if you access the Class C object properties, you should first find class A in order of declaration, and then look for class A. monotonicity of: If in the parse order of C, a is in front of B, the order must also be satisfied in all subclasses of C. ------------------------------the order in which properties are found in the new and legacy classes-------------------------------------in the modern class, when looking for a function or property to invoke, Is the breadth of first search. In the old class, it is a depth-first search.

Python Super Inheritance

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.