staticmethod makes my brain hurt

Devin Jeanpierre jeanpierreda at gmail.com
Thu Nov 17 01:20:43 EST 2011


> However, the fix is not as simple as merely making staticmethod objects
> callable. This was discussed at the 2011 language summit:
>
> http://www.boredomandlaziness.org/2011/03/python-language-summit-rough-notes.html
>
> See also this thread:
>
> http://mail.python.org/pipermail/python-dev/2011-March/109090.html

The notes didn't actually mention what was discussed, but re the
second thread, the issue was not changing staticmethod to be callable.
Making staticmethod callable is fine, but __get__ still has to return
the original function, not self.

The context of the second thread was that staticmethod was imagined as
one way to eliminate the difference between C and Python functions,
when added to a class. But this doesn't quite work, unless
staticmethod.__get__ returned self (which broke for related reasons).
If it returns the original function (as it does now) then the issue is
unresolved: C functions still behave differently from Python
functions, so you can't quite just hide them behind a staticmethod and
let everything remain the same. It doesn't eliminate the difference in
behavior in certain circumstances, e.g.:

    class MyClass:
        foo = staticmethod(foo)

    class MyOtherClass:
        foo = MyClass.foo

    MyOtherClass().foo() # what happens if foo is builtin vs pure-Python?

In the context of this thread, though, just making it callable without
modifying __get__ is fine. Or at least, I don't see the issue.

Devin

On Wed, Nov 16, 2011 at 11:43 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Wed, 16 Nov 2011 21:30:57 -0500, Roy Smith wrote:
>
>> When I run this (python 2.6.1):
>>
>> class C:
>>     @staticmethod
>>     def foo():
>>         pass
>>     print "inside", foo, callable(foo)
>>
>> print "outside", C.foo, callable(C.foo)
>>
>> I get:
>>
>> inside <staticmethod object at 0x421df0> False
>> outside <function foo at 0x41e6f0> True
>>
>> I don't understand.  Why is foo not callable inside of the class
>> definition?
>
>
> This has come up before.
>
> http://bytes.com/topic/python/answers/34396-static-method-object-not-callable
>
> http://bytes.com/topic/python/answers/462734-make-staticmethod-objects-callable
>
>
> However, the fix is not as simple as merely making staticmethod objects
> callable. This was discussed at the 2011 language summit:
>
> http://www.boredomandlaziness.org/2011/03/python-language-summit-rough-notes.html
>
> See also this thread:
>
> http://mail.python.org/pipermail/python-dev/2011-March/109090.html
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list