What are the uses of super() and __init__() methods in python

silencement
Release: 2020-09-19 14:04:55
Original
7475 people have browsed it

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.

What are the uses of super() and __init__() methods in python

__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

What are the uses of super() and __init__() methods in python

Result:

What are the uses of super() and __init__() methods in python

#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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!