class A(object): # A must be new-style class def __init__(self): print "enter A" print "leave A" class B(C): # A --> C def __init__(self): print "enter B" super(B, self).__init__() print "leave B"
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 converts the object self of class B for an object of class A, and then the "converted" object of class A 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:
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(), 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 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, the class type sequence of all base classes of a class is recorded in mro. 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
class A(object): def __init__(self): print "enter A" super(A, self).__init__() # new print "leave A" class B(object): def __init__(self): print "enter B" super(B, self).__init__() # new 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" super(E, self).__init__() # change print "leave E" class F(E, D): def __init__(self): print "enter F" super(F, self).__init__() # change print "leave F"
f = F(), execution result:
enter F enter E enter B enter C enter D enter A leave A leave D leave C leave B leave E leave F
It can be seen that the initialization of F not only completes the calls of all parent classes, but also ensures that the initialization function of each parent class is only called once.
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 instance;
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__, the order of the elements inside is found to be 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 sequence is implemented involves the implementation of the mro sequence. Versions after Python 2.3 use an algorithm called C3, which will be introduced in the next blog.