staticmethod makes my brain hurt
Dotan Cohen
dotancohen at gmail.com
Thu Nov 17 01:44:23 EST 2011
On Thu, Nov 17, 2011 at 04:30, Roy Smith <roy at panix.com> wrote:
> 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
Try this (untested):
class C:
@staticmethod
def foo():
pass
print "inside", C.foo, callable(C.foo)
--
Dotan Cohen
http://gibberish.co.il
http://what-is-what.com
More information about the Python-list
mailing list