The __init__ of the python3.6 child class calls the __init__ of the parent class
Parent class
class Worker: def __init__ (self): self.a=1 self.b=2if__name__= ="__main__ ": worker ()
Sub-class
fromtest.test02ImportworkerclassWorkertet (worker):def __init__(self): worker.__init__(self) C= 3D= 4Print(SELF.A)Print(self.b)Print(c)Print(d)defTest (self):Print(SELF.A)Print(self.b)if __name__=="__main__": Workertet ()
Output:
C:\Users\lys-tbc\AppData\Local\Programs\Python\Python36\python.exe d:/pythonwakce/mysqltest/test/test03-init.py
1
2
3
4
Process finished with exit code 0
The reason for this design is that you need to get the members and methods of the superclass in the subclass, and by calling the __init__ method of the superclass in the __init__ method of the subclass and manually passing it the self value to the subclass, you can make the __init__ method of the superclass set the initialized variable to a variable of subclass , so that the variables of the superclass can be accessed directly in the subclass.
The __init__ of the python3.6 child class calls the __init__ of the parent class