__New__ and the difference __init__: Python

This __new__ really rarely seen, do first understand it.

__new__是一个静态方法,而__init__是一个实例方法.
__new__方法会返回一个创建的实例,而__init__什么都不返回.
只有在__new__返回一个cls的实例时后面的__init__才能被调用.
当创建一个新实例时调用__new__,初始化一个实例时用__init__.

stackoverflow

PS: metaclass__ is active when you create a class so we can use __metaclass separately. , __ new__ and __init__ respectively in class creation, instance creation and instance initialization time to do some small hands and feet.

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/91610176