Is this a bug, or is it me?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Jan 18 17:39:08 EST 2008
On Fri, 18 Jan 2008 11:01:29 -0800, cptnwillard wrote:
> Now here is another one for your enjoyment:
>
> class C:
> @staticmethod
> def f1(): pass
> F = { '1' : f1 }
>
> C().F['1']()
>
>>>> TypeError: 'staticmethod' object is not callable
>
>
> What do you think of this one?
The trick is to realise that the object stored in the dict is not the
same object that you get back when you call C.f1().
>>> C().f1 is C().F['1']
False
>>> C().f1
<function f1 at 0xb7ec4a04>
>>> C().F['1']
<staticmethod object at 0xb7ee57dc>
What you've done in inadvertently bypassed Python's method-access
mechanism:
>>> C.__dict__['f1'] is C().f1
False
>>> C.__dict__['f1'] is C().F['1']
True
Analogous results will occur without the decorator (although the error
you get is different):
>>> class D:
... def foo(self): pass
... F = {'oo': foo}
...
>>> D().foo()
>>> D.F['oo']()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 1 argument (0 given)
So... not a bug, a feature :)
--
Steven
More information about the Python-list
mailing list