According to the docs,
__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.
It explicitly isn't a classmethod, but generally looks like one, except that any client manually calling __new__
needs to do explicitly pass in the class argument. For example:
>>> str.__new__()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
str.__new__()
TypeError: str.__new__(): not enough arguments
>>> str.__new__(str)
''
But alternate object creation APIs - eg, all eight alternate datetime
constructors - are usually classmethod
s, so that datetime.now()
works as expected.
Why is __new__
set up this way?