1 2 3 4 5 6 7 8 9 10 |
|
In our impression, super(B, self).__init__() is understood like this: super(B, self) first finds the parent class of B (that is, class A), and then puts class B into The object self is converted to an 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 (let’s not worry about whether the class architecture is reasonably designed, just study this example as a topic), the code is as follows
Code segment 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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 were called twice, which is not the result we expected! The expected result is that the initialization function of class A is called at most twice - in fact, this is a problem that multiple inheritance class systems must face. We draw the class system of code segment 4, as shown below:
object
|
| A
| It can be seen from the figure that when the initialization function of class C is called, the initialization function of class A should be called, but in fact the initialization function of class D is called. What a weird question!
In other words, mro records the class type sequence of all base classes of a class. Looking at the record of mro, we found that it contains 7 elements and the 7 class names are:
F E B C D A object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Summary
1. super is not a function, but a class name. The form super(B, self) actually calls the initialization function of the super class,
and generates a super object;
2. The super class The initialization function does not do any special operations, it simply records the class type and specific instances;
3. The call to super(B, self).func is not used to call the func function of the parent class of the current class;
5. Mix super classes and non- Binding functions is a dangerous behavior, which may result in the parent class function that should be called not being called or a parent class function being called multiple times.
Some more in-depth questions: As you can see, when printing F.__mro__, it is found that the order of the elements inside is F E B C D A object. This is the base class search order of F. As for why it is in this order, and python’s built-in multiple inheritance How the order is implemented, this involves the implementation of mro order,