The functions of super() and __init__() methods in python: __init__() method is used to create instance variables of objects, and super() method is used to call methods of the parent class.
__init__() is generally used to create instance variables of an object, or for one-time operations. super() is used to call the method of the parent class and can be used to solve the problem of multiple inheritance. In single inheritance, super() and __init__() have similar functions.
(Recommended tutorial: python video tutorial)
Main difference:
Using super() inheritance does not require explicit reference to the base class , which facilitates maintenance when the parent class changes;
super() can only be used in new-style classes and needs to inherit the object object. In the case of multiple inheritance, the inheritance order will be involved. Directly calling the parent class method using the class name will involve problems such as search order and repeated calls. super() returns the next class in the inheritance sequence, not the parent class.
The functions implemented by super() and __init__() in single inheritance are similar
Result:
#The difference is that you do not need to explicitly reference the base class when using super() inheritance.
Super is not the parent class, but the next class in the inheritance sequence. In the case of multiple inheritance, the inheritance order is involved. super() is equivalent to returning the next class in the inheritance order, not the parent class
The above is the detailed content of What are the uses of super() and __init__() methods in python. For more information, please follow other related articles on the PHP Chinese website!