Class Methods Vs Any Other Callable

Arnaud Delobelle arnodel at googlemail.com
Thu May 15 07:09:53 EDT 2008



bruno.desthuilliers at gmail.com wrote:
> On 14 mai, 22:44, Arnaud Delobelle <arno... at googlemail.com> wrote:
> > "bruno.desthuilli... at gmail.com" <bruno.desthuilli... at gmail.com> writes:
> > > On 14 mai, 19:45, Arnaud Delobelle <arno... at googlemail.com> wrote:
> > >> __new__ is a static method!
> >
> > > __new__ is a special-cased staticmethod that  1/ must not be declared
> > > as such and 2/ takes the class object as first args. As far as I'm
> > > concerned, it's semantically a classmethod.
> >
> > It's a static method!
>

OK then let me reply pedantically:

> Sorry Arnaud, I probably didn't made it clear enough : I do know it is
> a staticmethod object. What I say is that
> 1/ it's special-cased since you don't *explicitely* declare it as a
> staticmethod

In some cases you have to:

class Foo(object): pass

# Later on I want to redefine Foo.__new__

@staticmethod
def Foo__new__(cls):
    print "new Foo!"
    return object.__new__(cls)

Foo.__new__ = Foo__new__

>>> Foo()
New Foo!
>>> # Without the @staticmethod we would get a TypeError

> 2/ it behaves just like a classmethod, since it takes the class as
> first argument.

When you invoke it implicitely maybe, but not when you do so
explicitely! Look at my example below:  to create my Foo object, I
have to write

    object.__new__(Foo)

> IOW, I'm talking about semantic, not implementation.

I understand what you mean, and from that point of view you can argue
against my example above (after all, my example is code, not pure
concept) but for me it is simpler to think of __new__ as a
staticmethod (which it is!), plain and simple.

--
Arnaud



More information about the Python-list mailing list