instancemethod

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Jan 27 06:39:17 CET 2007


On Fri, 26 Jan 2007 20:27:29 -0800, Michele Simionato wrote:

> On Jan 22, 2:58 am, "Gert Cuykens" <gert.cuyk... at gmail.com> wrote:
> 
>> http://www.faqts.com/knowledge_base/view.phtml/aid/16824
> 
> There is a factual mistake on that reference. The last sentence
> 
>> One final note: the single most common use for classmethod is probably
>> in overriding __new__(). It is always needed when overriding __new__()
>> because __new__() is a classmethod.
> 
> is wrong, since __new__ is a staticmethod, NOT a classmethod.

There's a contradiction in the Python docs. 

>From http://docs.python.org/lib/built-in-funcs.html we see:

"A class method receives the class as implicit first argument, just like
an instance method receives the instance."

and 

"A static method does not receive an implicit first argument."


But from http://docs.python.org/ref/customization.html we read:

"__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.
    The remaining arguments are those passed to the object constructor
    expression (the call to the class)."

It seems that the description of __new__ is wrong. Since __new__ takes an
implicit first argument, the class, it is a class method, not a static
method.



-- 
Steven.




More information about the Python-list mailing list