andrew-pythonlist at puzzling.org
Wed Feb 26 01:06:34 CET 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>
>>> class A:
... def foo():
>>> A.foo = staticmethod(A.foo.im_func)
<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.
More information about the Python-list