Behavior of staticmethod in Python 3

Peter Otten __peter__ at web.de
Sat Nov 23 16:51:22 EST 2013


Antoon Pardon wrote:

> Op 23-11-13 10:01, Peter Otten schreef:
> 
>> 
>> Your script is saying that a staticmethod instance is not a callable
>> object. It need not be because
>> 
>> Foo.foo()
>> 
>> doesn't call the Foo.foo attribute directly, it calls
>> 
>> Foo.foo.__get__(None, Foo)()
> 
> I think you are burdening the programmer with implemantation details
> that don't matter to him.
> 
> IMO if Foo.foo() is legal then Foo.foo is callable. That the actual call
> is delegated to Foo.foo.__get__(None, Foo) shouldn't matter.

If you read the original post -- I think in this case the details do matter. 

What is your highlevel explanation for

>> class Foo:
...     @staticmethod
...     def foo(): pass
...     try: foo()
...     except Exception as err:
...             print(err)
... 
'staticmethod' object is not callable
>>> Foo.foo()

or maybe clearer:

>>> @staticmethod
... def foo(): pass
... 
>>> def bar(): pass
... 
>>> class Foo:
...     foo = foo
...     bar = bar
... 
>>> Foo.bar is bar
True
>>> Foo.foo is foo
False

How would you explain that without mentioning the descriptor protocol?




More information about the Python-list mailing list