staticmethod
Andrew Bennetts
andrew-pythonlist at puzzling.org
Tue Feb 25 19:06:34 EST 2003
On Tue, Feb 25, 2003 at 11:36:02PM +0000, Jeremy Yallop wrote:
> I find staticmethod()'s behaviour a bit surprising. In particular:
>
> Why does staticmethod() `work' on uncallable objects?
>
> >>> staticmethod('huh?')
> <staticmethod object at 0x8152eb8>
>
> I'd expect this to raise an exception.
>
> Why can't I make a class attribute into a staticmethod outside the
> class definition?
>
> >>> class A:
> ... def foo():
> ... pass
> ...
> >>> A.foo = staticmethod(A.foo)
> >>> A.foo
> <unbound method A.foo>
Try this:
>>> class A:
... def foo():
... pass
...
>>> A.foo = staticmethod(A.foo.im_func)
>>> A.foo
<function foo at 0x8156f64>
>>> A.foo() # This works
The problem is that you were trying to make a staticmethod with an unbound
method, not with the function. You could also use A.__dict__['foo'] instead
of A.foo.im_func, I think.
-Andrew.
More information about the Python-list
mailing list