【__new__】
object.
__new__
(cls[, ...])
Called to create a new instance of class cls. 用于创建类对象cls的实例。
__new__()
is a static method (special-cased so you need not declare it as such) __new__是一个特殊的方法,不需要声明为static方法。第一个参数是类对象。其余的参数会被传递给__init__。
If __new__()
returns an instance of cls, then the new instance’s __init__()
method will be invoked like __init__(self[, ...])
, where self is the new instance and the remaining arguments are the same as were passed to __new__()
.
If __new__()
does not return an instance of cls, then the new instance’s __init__()
method will not be invoked.
__new__()
is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.