[docs] [issue21415] Python __new__ method doc typo (it's a class and not a static method)

Steven D'Aprano report at bugs.python.org
Fri May 2 14:07:35 CEST 2014


Steven D'Aprano added the comment:

Actually, no, it is a staticmethod. See Guido's tutorial from way back in version 2.2:

[quote]
__new__ is a static method. When defining it, you don't need to (but may!) use the phrase "__new__ = staticmethod(__new__)", because this is implied by its name (it is special-cased by the class constructor).
[end quote]

https://www.python.org/download/releases/2.2.3/descrintro


I believe that this explains why you have to use this idiom inside __new__ when using super():

    def __new__(cls, x):
        super().__new__(cls, x)

If __new__ were a classmethod, the first argument "cls" would be provided automatically.


If you try making __new__ a classmethod, it breaks:

py> class Test:
...     @classmethod
...     def __new__(cls):
...             pass
...
py> x = Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() takes 1 positional argument but 2 were given

whereas a staticmethod works fine.

----------
nosy: +steven.daprano
resolution:  -> not a bug
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21415>
_______________________________________


More information about the docs mailing list