[Python-Dev] Making staticmethod objects callable?

Steven Bethard steven.bethard at gmail.com
Wed Mar 1 16:50:21 CET 2006


On 3/1/06, Nicolas Fleury <nidoizo at yahoo.com> wrote:
> Basically, should staticmethods be made callable so that the following
> would not raise an exception:
>
> class A:
>      @staticmethod
>      def foo(): pass
>      bar = foo()
>
> There's workarounds, but it's really just about usability.  staticmethod
> could still return a descriptor, but additionnally callable.  Is there
> something I'm missing?  Is it error-prone in any way?

My only (mild) concern is that if staticmethod is going to get a
__call__, I think classmethod should probably get one too.  Inside a
class this doesn't make much sense:

    class A(object):
        @classmethod
        def foo(cls):
            pass
        bar = foo(None) # ??

But I guess outside of a class maybe it's okay:

    @classmethod
    def foo(cls):
        pass

    class A(object):
        pass

    foo(A)

Anyway, my feeling was that running into this behavior (that
staticmethod is not callable) is a good oportunity to explain how
descriptors work.  And once you start playing around with staticmethod
and classmethod, you're going to need to learn that pretty soon
anyway.  Hiding it a little bit longer with a __call__ method on
staticmethod isn't going to help much in the long run.

So I guess I'm -0 if classmethod gets a __call__ too.

STeVe
--
Grammar am for people who can't think for myself.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list