staticmethod makes my brain hurt
Roy Smith
roy at panix.com
Wed Nov 16 21:30:57 EST 2011
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? Where this comes up is that I'm trying to use a callable
default in mongoengine:
class User(Document):
@staticmethod
def _get_next_id():
[blah, blah, blah]
return id
user_id = IntField(required=True, default=_get_next_id)
The way mongoengine works is if callable(default) is true, it calls
default() to get the real value to use. At the point where the
IntField() call is made, _get_next_id is not callable, and eventually I
end up with:
ValidationError: <staticmethod object at 0x2a3c1a0> could not be
converted to int
More information about the Python-list
mailing list