The initialization method for classes in Python is __init__ (), therefore, the initialization method of the parent class subclass is this, if the subclass does not implement this function, the initialization function of the parent class is invoked when the subclass implements this function, and the parent class is overwritten with this function, now that the parent class is inherited, It is necessary to explicitly call the __init__ () of the parent class in this function, unlike C++,java, who automatically invokes the parent class initialization function.
Calling the parent class function has the following methods:
Class A:
Def method (self, arg):
Pass
Class B (A):
Def method (self, arg):
# A.method (self,arg) # 1
# super (B, Self). Method (ARG) #2
Super (). Method (ARG) #3
1. Direct Write class name call
2. Called with super (type, obj). Method (ARG).
3. Call the parent class method of this class in the class definition directly to Super (). Methods (ARG).
Example of a method that calls the parent class of an object in code:
OB = B ()
Super (B,ob). Method (Arg) #调用class the method of parent Class A of B.
Example of invoking the parent class initialization method in initialization:
Class B (A):
def __init__ (self):
Super (). __INIT__ ()