Foo.__new__ is what species of method?
steve+comp.lang.python at pearwood.info
Tue Jul 14 06:54:07 CEST 2015
On Tuesday 14 July 2015 14:45, Ben Finney wrote:
> Howdy all,
> The Python reference says of a class ‘__new__’ method::
> 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.
This is correct. __new__ is a static method and you need to explicitly
provide the cls argument:
py> class Spam(object):
... def __new__(cls):
... print cls
py> Spam.__new__() # implicit first arg?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __new__() takes exactly 1 argument (0 given)
> I suspect this a bug in the reference documentation for ‘__new__’, and
> it should instead say “__new__ is a class method …”. Am I wrong?
I've made that mistake in the past too :-)
More information about the Python-list