python Medium super, Named superclass , It can be simply understood as executing the parent class __init__ function . Because in python Whether it's one-to-one inheritance , Or a subclass inherits multiple superclasses , Will involve the order of execution . So this article focuses on super The specific role of .
By designing such a case , We can make it clear that super The logical relationship between before and after : Define a parent class first initial, There are parameter values in this parent class param And the function func, Subclass then new To inherit the parent class initial. After inheritance , In subclass __init__ Function super Print parameter values before and after execution param And the function func The return value of , The relevant code is as follows :
# Define parent class
class initial(object):
def __init__(self):
print ('This print is from initial object')
# Define parent class parameters
self.param = 3
# Define the parent function
def func(self):
return 1
# Defining subclasses
class new(initial):
def __init__(self):
print ('This print is from new object')
# Print the subclass function value
print (self.func())
# Execute the parent class initialization function
super(new, self).__init__()
# Print the parent parameter value
print(self.param)
self.param = 4
# Define subclass functions
def func(self):
return 2
if __name__ == '__main__':
new()
The result of the code execution is as follows :
This print is from new object
2
This print is from initial object
3
First of all, we noticed , Parent class initial Medium __init__ Print statement in function , Is in super Then output , This illustrates the ,super Function is used to initialize the parent class . So if it's not implemented super,new Subclass pair initial Where is the inheritance of the parent class ? The answer is the member function of the parent class , For example, such a case :
'''
No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :660193417###
Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book !
'''
class initial(object):
def __init__(self):
print ('This print is from initial object')
self.param = 3
def func(self):
return 1
class new(initial):
def __init__(self):
print ('This print is from new object')
print (self.func())
super(new, self).__init__()
print(self.param)
self.param = 4
if __name__ == '__main__':
new()
In fact, the overloaded member functions in subclasses are deleted , Then the results are as follows :
This print is from new object
1
This print is from initial object
3
It can be found that super You can print the of the parent class before func The function value of the function . therefore python The logic of inheritance in is like this :
It is precisely because only execution super To initialize the member variables in the parent class , So if the super Previously, the member variables of the parent class could not be accessed .
In this paper, through a python The design of the actual case , To explain python Object oriented technology —— Class must be used in the inheritance of super The logic of a function . In fact, we can python The inheritance of classes in is understood as such a process : When we specify the parent class in parentheses , In fact, the member function of the parent class has been referenced , But the initialization function of the parent class is not executed . While executing the initialization function of the subclass , It will check whether the member function of the parent class is overloaded , If overloaded, it will directly overwrite . Only when super after , It is equivalent to executing the initialization function of the parent class , Only then can the member variables of the parent class be accessed .