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

eryksun report at bugs.python.org
Fri May 2 16:45:24 CEST 2014


eryksun added the comment:

> 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)

Yes, if __new__ is defined and is a function, type_new replaces it with a staticmethod:

http://hg.python.org/cpython/file/04f714765c13/Objects/typeobject.c#l2437

For example:

    >>> class A: __new__ = lambda c: 0

    >>> type(vars(A)['__new__'])
    <class 'staticmethod'>

A heap type that defines __new__ has tp_new set to slot_tp_new. This looks up and calls __new__, with the class inserted as the first argument:

http://hg.python.org/cpython/file/04f714765c13/Objects/typeobject.c#l6036

If you use a classmethod, looking up __new__ returns a method bound to the class. When called, this inserts the class in the args yet again:

http://hg.python.org/cpython/file/04f714765c13/Objects/classobject.c#l321

----------
nosy: +eryksun

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


More information about the docs mailing list