Typical implementations create a new instance of the class by invoking the superclass’s
__new__()
method usingsuper(currentclass, cls).__new__(cls[, ...])
with appropriate arguments and then modifying the newly-created instance as necessary before returning it.
...If
__new__
does not return an instance of cls, then the new instance’s__init__()
method will not be invoked.
The simplest implementation of __new__
:
class MyClass:
def __new__(cls):
RetVal = super(currentclass, cls).__new__(cls)
return RetVal
How exactly does super(currentclass, cls).__new__(cls[, ...])
return an object of type cls
?
That statement calls object.__new__(cls)
where cls
is MyClass
.
So how would class object
know how to create the type MyClass
?