Call details of super in multi-inheritance

Source: Internet
Author: User
Note: python3 is the runtime environment. The example is taken from Chapter 8th of pythoncookbook. In python, there are two main methods to initialize the parent class if the subclass is to be implemented. The first method is to directly use the parent class name, and the second method... & quot; & gt; & lt; metaname & quot; keywords & quot; content & quot; call details of pythonsuper in multi-inheritance. note: python 3 is the runtime environment, the example is taken from Chapter 8th of python cookbook.

In python, there are two main methods to initialize the parent class if the subclass is to implement the initialization. The first is to directly use the parent class name, and the second is to use the super method. There is no difference between the two in single inheritance, but some slight gaps need to be noted during multi-inheritance. The instance explanation is the truth!
1. use the parent class name:

Python code

Class Base:

Def _ init _ (self ):

Print ('base. _ init __')

Class A (Base ):

Def _ init _ (self ):

Base. _ init _ (self)

Print ('A. _ init __')

Class B (Base ):

Def _ init _ (self ):

Base. _ init _ (self)

Print ('B. _ init __')

Class C (A, B ):

Def _ init _ (self ):

A. _ init _ (self)

B. _ init _ (self)

Print ('C. _ init __')


The following output is returned when class C is instantiated:

Python code

>>> C = C ()

Base. _ init __

A. _ init __

Base. _ init __

B. _ init __

C. _ init __

>>>


It can be seen that the Base class has been called twice. This is probably not the result we want in many cases, so we can consider using the super method at this time.

2. use super:

Python code

Class Base:

Def _ init _ (self ):

Print ('base. _ init __')

Class A (Base ):

Def _ init _ (self ):

Super (). _ init __()

Print ('A. _ init __')

Class B (Base ):

Def _ init _ (self ):

Super (). _ init __()

Print ('B. _ init __')

Class C (A, B ):

Def _ init _ (self ):

Super (). _ init _ () # Only one call to super () here

Print ('C. _ init __')


In this case, the output of class C is instantiated as follows:

Python code

>>> C = C ()

Base. _ init __

B. _ init __

A. _ init __

C. _ init __

>>>


We can see that the Base class is called only once! Unfortunately, this is not the reason why I wrote this blog record, because if you observe it carefully, although the Base class is indeed called only once as expected, but did you find that "B. _ init _ "before". _ init __"? Why does this make the Base only initialize once? Maybe you're a little aggressive, right? In fact, all this has to be "blamed" for super's call process when there are multiple inheritance classes. When python implements a class (not only inheritance), it will generate a method to generate a resolution sequence list, which can be viewed by class attribute _ mro, in this example:

Python code

>>> C. _ mro __

( , , ,

, )

>>>


When you search for an attribute or method, it traverses each class according to the list until the first class that matches the property or method is found. When super is used in inheritance, the interpreter searches for the next class on the list every time super is encountered until super or list traversal is completed, then return layer-by-layer recursively. Therefore, the search process in this example is as follows: in C, the next class in the super --> Search List is encountered, that is, super is encountered again in A --> A, and super is reproduced in B --> B, search Base --> initialize Base class and return recursively.
To better explain this process, comment out the super row of Class B:

Python code

Class B (Base ):

Def _ init _ (self ):

# Super (). _ init __()

Print ('B. _ init __')

Class C (A, B ):

Def _ init _ (self ):

Super (). _ init _ () # Only one call to super () here

Print ('C. _ init __')


Instantiate Class C again, and the output is as follows:

Pythonn code

>>> C = C ()

B. _ init __

A. _ init __

C. _ init __


Base class no longer produces output! Why? Because B does not have super, it blocks the list to search for the Base class, so it does not initialize the Base!

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.