function super() Used for subclass calling parent class ( Superclass ) Methods .
You must ask why you use functions in subclasses super() To call the method in the parent class ?
Can't I use it like this ?
class A:
def fun1(self):
print('Enter A')
class B(A):
def fun1(self):
A.fun1(self)
print('Enter B')
object1 = B()
object1.fun1()
The operation results are as follows :
yes , It can be like this , But the disadvantage of this is , When the parent of a subclass changes ( Such as class B The parent of the A Turn into C when ), You must traverse the entire B The definition of a class , Change all relevant call names .
The above example is very simple , There is only one point that needs to be modified , The above shortcomings are not enough to be a problem , But when modern yards are huge , Such a change could be catastrophic .
Python Built in functions are provided in super() To overcome this shortcoming and problem , Take a look at using built-in functions super() To call a superclass method .
class A:
def fun1(self):
print('Enter A')
class B(A):
def fun1(self):
super(B, self).fun1()
print('Enter B')
object1 = B()
object1.fun1()
The operation results are as follows :
Through the code above , We can see that , If a class B Parent class of A Change it to C, You just need to put the first sentence in the class definition :
from
class B(A):
Change it to
class B(C):
That's it , Without changing the relevant call statements .
stay Python3 in , sentence
super(B, self).fun1()
I could just write it as :
super().fun1()
From above , function super() Although the name of the parent class is hidden , Make the method of calling the parent class independent of the name of the parent class , But it also brings problems . What's the problem ? See the following example :
class A:
def fun1(self):
print('Enter A')
class B(A):
def fun1(self):
A.fun1(self)
print('Enter B')
class C(A):
def fun1(self):
A.fun1(self)
print('Enter C')
class D(B, C):
def fun1(self):
B.fun1(self)
print('Enter D')
object1 = D()
object1.fun1()
Through the code above , We can see the subclasses clearly D There are two parents B and C, namely D Is a multiple inheritance . stay D Method of invocation fun1() Parent class B The method in fun1(), So the results are as follows :
But if we use a function super Hide the parent class name , That's the problem , The code is as follows :
class A:
def fun1(self):
print('Enter A')
class B(A):
def fun1(self):
super().fun1()
print('Enter B')
class C(A):
def fun1(self):
super().fun1()
print('Enter C')
class D(B, C):
def fun1(self):
super().fun1()
print('Enter D')
object1 = D()
object1.fun1()
The problem with the above code is the class D In the parent class fun1 I don't know which parent class to call fun1. function super() This is how to deal with this problem : use MRO Resolve all the parent classes to in sequence fun1 Run it all over again and each method found fun1 Only run once .
We can use the following statement to put the class D Of MRO Print out in sequence :
print(D.__mro__)
The sample code is as follows :
class A:
def fun1(self):
print('Enter A')
class B(A):
def fun1(self):
super().fun1()
print('Enter B')
class C(A):
def fun1(self):
super().fun1()
print('Enter C')
class D(B, C):
def fun1(self):
super().fun1()
print('Enter D')
print(D.__mro__) # Ba class D Of MRO Print out in sequence
The operation results are as follows :
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
According to the above MRO The order , We can know the class D The order of querying a method is class D→ class B→ class C→ class A.
According to the above order , We have the following about object1.fun1() The execution of this statement :
object1.fun1() The first statement that needs to be executed is class D The statement in super().fun1(), This statement is executed after :
First, in class D Find a way fun1(), eureka , Once found, the class D The method in fun1() The number is ①
Then in the class B Find a way fun1(), eureka , Once found, the class B The method in fun1() The number is ②
Then in the class C Find a way fun1(), eureka , Once found, the class C The method in fun1() The number is ③
Then in the class A Find a way fun1(), eureka , Once found, the class A The method in fun1() The number is ④
And then according to ④→③→②→① The sequence is executed once, one by four fun1()
When executed ④ when , The only statement in the function body “print(‘Enter A’)” Print out the string Enter A
④ Execute after execution ③, When executed ③ when , The first statement is super().fun1(), So search in the following order fun1:
First, in class C Find a way fun1(), eureka , Find the class after finding C The method in fun1() Has been numbered ③
Then in the class A Find a way fun1(), eureka , Find the class after finding A The method in fun1() Has been numbered ④
And then according to ④→③ The sequence is executed once, one by two fun1(), Preparing to execute ④ When , Find out ④ It has been executed once , So no more execution ④, So directly execute ③, perform ③ I found the first sentence when I was reading super().fun1() It has been executed once , So don't execute this statement again , Then execute the statement print(‘Enter C’), Print out the string Enter C, thus ③ completion of enforcement .
③ Execute after execution ②, When executed ② when , The first statement is super().fun1(), So search in the following order fun1:
First, in class B Find a way fun1(), eureka , Find the class after finding B The method in fun1() Has been numbered ②
Then in the class A Find a way fun1(), eureka , Find the class after finding A The method in fun1() Has been numbered ④
And then according to ④→② The sequence is executed once, one by two fun1(), Preparing to execute ④ When , Find out ④ It has been executed once , So no more execution ④, So directly execute ②, perform ② I found the first sentence when I was reading super().fun1() It has been executed once , So don't execute this statement again , Then execute the statement print(‘Enter B’), Print out the string Enter B.
② Execute after execution ①, When executed ① when , The first statement is super().fun1(), This statement has already been executed , So don't execute this statement again , Then execute the remaining statement print(‘Enter D’), Print out the string Enter D.
Sum up , The output information should be as follows :
Enter A
Enter C
Enter B
Enter D
Let's see if the running result of the program is like this :
From the screenshot above , The results of the actual operation of the program are consistent with our analysis results , So our analysis is correct .