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: 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: References: Date: Thu, 17 Nov 2011 08:44:23 +0200 Subject: Re: staticmethod makes my brain hurt From: Dotan Cohen To: Roy Smith 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Nov 17, 2011 at 04:30, Roy Smith wrote: > When I run this (python 2.6.1): > > class C: > =C2=A0 =C2=A0@staticmethod > =C2=A0 =C2=A0def foo(): > =C2=A0 =C2=A0 =C2=A0 =C2=A0pass > > =C2=A0 =C2=A0print "inside", foo, callable(foo) > > print "outside", C.foo, callable(C.foo) > > I get: > > inside False > outside True > > I don't understand. =C2=A0Why is foo not callable inside of the class > definition? =C2=A0Where this comes up is that I'm trying to use a callabl= e > default in mongoengine: > > class User(Document): > =C2=A0 =C2=A0@staticmethod > =C2=A0 =C2=A0def _get_next_id(): > =C2=A0 =C2=A0 =C2=A0[blah, blah, blah] > =C2=A0 =C2=A0 =C2=A0return id > > =C2=A0 =C2=A0user_id =3D IntField(required=3DTrue, default=3D_get_next_id= ) > > The way mongoengine works is if callable(default) is true, it calls > default() to get the real value to use. =C2=A0At the point where the > IntField() call is made, _get_next_id is not callable, and eventually I > end up with: > > ValidationError: could not be > converted to int Try this (untested): class C: @staticmethod def foo(): pass print "inside", C.foo, callable(C.foo) --=20 Dotan Cohen http://gibberish.co.il http://what-is-what.com