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


Groups > comp.lang.python > #15808

Re: staticmethod makes my brain hurt

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <dotancohen@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '(python': 0.05; 'callable': 0.09; 'foo': 0.09; 'foo,': 0.09; 'def': 0.14; 'cc:addr :python-list': 0.15; 'callable,': 0.16; 'made,': 0.16; 'roy': 0.16; 'received:74.125.82.44': 0.17; 'received:mail- ww0-f44.google.com': 0.17; 'wrote:': 0.18; 'converted': 0.18; 'int': 0.18; 'trying': 0.21; 'cc:no real name:2**0': 0.21; 'header :In-Reply-To:1': 0.23; 'cc:2**0': 0.25; "i'm": 0.26; 'pass': 0.28; 'true,': 0.28; 'cc:addr:python.org': 0.29; 'message- id:@mail.gmail.com': 0.29; 'print': 0.29; 'class': 0.29; 'nov': 0.31; 'thu,': 0.32; '17,': 0.34; 'skip:@ 10': 0.34; 'received:74.125.82': 0.37; 'could': 0.38; 'run': 0.38; 'received:google.com': 0.38; 'received:74.125': 0.39; 'subject:: ': 0.39; 'skip:_ 10': 0.40; '2011': 0.62; 'url:co': 0.62; 'skip:\xc2 10': 0.73
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=rwcjZQ4Vv5MRoASN+b2jb36RFqOPqfvQ8L03U9XymBQ=; b=xvhbFYHbI32ST2TTepma9C0zczrPVvw6egitTqper0l1mcc6/ltNsGHp5QfZFyevYB n4vVzjNTabFMT5uAyZdmWXS77smj4nqS7ETLUZ1JDrxJ+5HgpoC1wMCElM5VR3qoIcCV cwDPlLTbZDNhho9mMgKth1smrM0i/doS2kc0E=
MIME-Version 1.0
In-Reply-To <roy-DD1C5B.21305716112011@news.panix.com>
References <roy-DD1C5B.21305716112011@news.panix.com>
Date Thu, 17 Nov 2011 08:44:23 +0200
Subject Re: staticmethod makes my brain hurt
From Dotan Cohen <dotancohen@gmail.com>
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2792.1321512266.27778.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321512266 news.xs4all.nl 6897 [2001:888:2000:d::a6]:54686
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15808

Show key headers only | 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