Python 3-8 magic methods and built-in functions

1. __init__ and __new__ 

In python, the construction method includes creating an object and initializing an object . The __new__ method is executed first, and then the __init__ method is executed.

__new__ is called before the instance is created, because its task is to create an instance and then return it. It is a static method for customizing objects.

The return value (instance) of __new__ will be passed to the first parameter of the __init__ method, and then __init__ will set some parameters for this instance.

__init__ is called when the instance object is created, and then sets some initial values ​​of the object properties.

__new__ is usually used to control the process of generating a new instance. It is a class-level method.

__init__ is usually used to initialize a new instance and control the initialization process, such as adding some attributes and doing some extra operations, which occur after the class instance is created. It is an instance-level method.

class object

Guess you like

Origin blog.csdn.net/weixin_43955170/article/details/113105708