一.__new__()方法
官方文档:
object.__new__(cls[, ...])Called to create a new instance of class cls.
__new__()is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of__new__()should be the new object instance (usually an instance of cls).Typical implementations create a new instance of the class by invoking the superclass’s

本文详细介绍了Python中__new__()和__init__()的区别。__new__()是一个静态方法,主要用来创建实例,它必须返回一个类的实例,而__init__()则在__new__()之后调用,用于初始化新创建的实例。两者协同工作以构造对象,__new__()创建对象,__init__()对其进行定制。如果不返回类的实例,__init__()将不会执行。
最低0.47元/天 解锁文章





