Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15808

Re: staticmethod makes my brain hurt

References <roy-DD1C5B.21305716112011@news.panix.com>
Date 2011-11-17 08:44 +0200
Subject Re: staticmethod makes my brain hurt
From Dotan Cohen <dotancohen@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2792.1321512266.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Nov 17, 2011 at 04:30, Roy Smith <roy@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-16 21:30 -0500
  Re: staticmethod makes my brain hurt alex23 <wuwei23@gmail.com> - 2011-11-16 18:44 -0800
    Re: staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-16 21:52 -0500
  Re: staticmethod makes my brain hurt Ethan Furman <ethan@stoneleaf.us> - 2011-11-16 19:24 -0800
    Re: staticmethod makes my brain hurt alex23 <wuwei23@gmail.com> - 2011-11-17 18:31 -0800
  Re: staticmethod makes my brain hurt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-17 04:43 +0000
    Re: staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-17 00:26 -0500
    Re: staticmethod makes my brain hurt Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-11-17 01:20 -0500
  Re: staticmethod makes my brain hurt Dotan Cohen <dotancohen@gmail.com> - 2011-11-17 08:44 +0200
  Re: staticmethod makes my brain hurt Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-17 00:37 -0700
  Re: staticmethod makes my brain hurt Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-11-17 10:17 +0100
  Re: staticmethod makes my brain hurt Dotan Cohen <dotancohen@gmail.com> - 2011-11-17 13:13 +0200
  Re: staticmethod makes my brain hurt Chris Angelico <rosuav@gmail.com> - 2011-11-17 22:38 +1100

csiph-web