Python multi-inheritance (New Class) iii

Source: Internet
Author: User

Go deep into super

The following content is referenced from: Signature.

Code segment 3
 A(object):             B(C):          

In our impression, for super (B, self ). _ init _ () is as follows: super (B, self) first finds the parent class of B (Class ), then, convert the self object of Class B to the object of Class A, and then the "converted" Class A object calls its own _ init _ function.

One day, a colleague designed a relatively complex class architecture (we should not care whether this class system is designed properly, just take this example as a question to study). The Code is as follows:
Code segment 4:
                             

F = F (). The result is as follows:

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

Obviously, the initialization functions of Class A and Class D are repeatedly called twice, which is not the expected result! The expected result is that only the initialization function of Class A is called twice. In fact, this is A problem that must be faced by the multi-inheritance class system. We will draw out the class system of code segment 4, such:

Object
| \
|
|/|
B C D
\/|
E |
\ |
F

According to our understanding of super, we can see that when calling Class C's initialization function, we should call class A's initialization function, but actually called Class D's initialization function. A strange problem!

That is to say, mro records the sequence of class types of all base classes of a class. Check the mro records and find that there are 7 elements and the seven class names are:

F e B C D A object

This explains why super (C, self). _ init _ () is used in C. _ init _ to call the initialization function of class D. ???

We rewrite code segment 4:

Code segment 5:
  ()         ()                 ()         ()      

F = F (), execution result:

Enter F enter E enter B enter C enter D enter A leave D leave C leave B leave E leave F

It can be seen that the initialization of F not only completes the call of all parent classes, but also ensures that the initialization function of each parent class is called only once.

Summary

1. super is not a function. It is a class name. For example, super (B, self) actually calls the initialization function of the super class,
Generates a super object;
2. The super class initialization function does not perform any special operations, but simply records the class type and specific instance;
3. The call of super (B, self). func is not used to call the func function of the parent class of the current class;
4. The multiple inheritance classes of Python use mro to ensure that the functions of each parent class are called one by one, and each parent class function is also guaranteed.
Only one call (if each class uses super );
5. mixing super classes and unbound functions is a dangerous action, which may cause the parent class function to be called not to be called or
Parent functions are called multiple times.

Some more in-depth questions: You can see that the order of elements in print F. _ mro _ is

 

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.