official site:
https://docs.python.org/3/reference/datamodel.html
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 __new__()
method using super().__new__(cls[, ...])
with appropriate arguments and then modifying the newly-created instance as necessary before returning it.
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.
object._ init _(self[, ...])
Called after the instance has been created (by __new__()
), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an __init__()
method, the derived class’s __init__()
method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: super().__init__([args...])
.
Because __new__()
and __init__()
work together in constructing objects (__new__()
to create it, and __init__()
to customize it), no non-None
value may be returned by __init__()
; doing so will cause a TypeError
to be raised at runtime.
object._ del _(self)
Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a __del__()
method, the derived class’s __del__()
method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance.
It is possible (though not recommended!) for the __del__()
method to postpone destruction of the instance by creating a new reference to it. This is called object resurrection. It is implementation-dependent whether __del__()
is called a second time when a resurrected object is about to be destroyed; the current CPythonimplementation only calls it once.
It is not guaranteed that __del__()
methods are called for objects that still exist when the interpreter exits.