Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'instance': 0.05; 'compiler': 0.07; 'interpreter': 0.07; 'method,': 0.07; 'wrapper': 0.07; 'called.': 0.09; 'created,': 0.09; 'instance.': 0.09; 'invocation': 0.09; 'stack,': 0.09; 'storing': 0.09; 'am,': 0.12; 'stored': 0.13; "subject:' ": 0.15; 'bound,': 0.16; 'frame,': 0.16; 'function?': 0.16; 'somehow,': 0.16; 'cc:addr :python-list': 0.16; 'possibly': 0.16; 'question.': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'programming': 0.20; 'cc:no real name:2**0': 0.20; 'figure': 0.21; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'obviously': 0.23; 'sep': 0.23; 'changed': 0.24; 'variable': 0.24; 'stack': 0.24; 'code': 0.25; "i'm": 0.27; 'subject:need': 0.28; 'bound': 0.29; 'rid': 0.29; 'work:': 0.29; 'message-id:@mail.gmail.com': 0.29; 'second': 0.29; 'asking': 0.29; 'cc:addr:python.org': 0.30; 'class': 0.30; 'subject:?': 0.31; 'received:209.85.161.46': 0.31; 'received:mail- fx0-f46.google.com': 0.31; 'does': 0.32; 'it.': 0.33; 'probably': 0.33; 'there': 0.33; '...': 0.34; 'function.': 0.34; 'latter': 0.34; 'rather': 0.35; 'received:209.85.161': 0.35; 'fri,': 0.36; 'created': 0.36; 'another': 0.37; 'later,': 0.37; 'variables': 0.37; 'options': 0.37; 'two': 0.37; 'could': 0.38; 'allows': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'ways': 0.39; 'called': 0.40; "it's": 0.40; 'period': 0.61; 'john': 0.62; '11:51': 0.84; 'remembering': 0.84; 'subject:always': 0.84; 'subject:class': 0.84; 'subject:self': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=LoOGuR/7kmCpl8xfVoER3YCLGj4LGyKs5p8KtQZepzs=; b=UpZUUw+CwiFBdjBq23mdCbOu6zxlDVLsLY0Wlh55bDWwdKYACs4KFsrCmRGMetFxUh 8iAyCR4gS84Q8uyM0QNWaY+2OQXJRLn5XmlKlP5bpgfPfST68A/Ze2taV1aQbKc7A4AS XBY6M/UOom83s9vj8Mf3zEmHxgrQF4xW77uVI= MIME-Version: 1.0 In-Reply-To: <975cd42a-00b0-4d2e-9f22-95b4021a7fbb@g32g2000pri.googlegroups.com> References: <0dc26f12-2541-4d41-8678-4fa53f347acf@g9g2000yqb.googlegroups.com> <42e335a7-b872-4229-ae02-13d61b7fab35@w22g2000prj.googlegroups.com> <975cd42a-00b0-4d2e-9f22-95b4021a7fbb@g32g2000pri.googlegroups.com> From: Ian Kelly Date: Fri, 2 Sep 2011 14:30:14 -0600 Subject: Re: Why do class methods always need 'self' as the first parameter? To: John Roth Content-Type: text/plain; charset=ISO-8859-1 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314995446 news.xs4all.nl 2411 [2001:888:2000:d::a6]:50777 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12674 On Fri, Sep 2, 2011 at 11:51 AM, John Roth wrote: >> I don't see how you could get rid of the wrappers. =A0Methods would >> still need to be bound, somehow, so that code like this will work: >> >> methods =3D {} >> for obj in objs: >> =A0 =A0 if obj.is_flagged: >> =A0 =A0 =A0 =A0 methods[obj.user_id] =3D obj.do_work >> =A0 =A0 else: >> =A0 =A0 =A0 =A0 methods[obj.user_id] =3D obj.do_other_work >> # ... >> methods[some_user_id]() >> >> Without method wrappers, how does the interpreter figure out which >> instance is bound to the method being called? >> >> Cheers, >> Ian > > Good question. > > Currently the instance wrapper is created during method instantiation, > so the instance is obviously available at that point. There are two > rather obvious ways of remembering it. One is to use the invocation > stack, which has the instance. Another would be for the compiler to > create a local variable for the instance and possibly the class and > fill them in at instantiation time. Both of these require fixing the > names "self" and "cls" so the compiler knows what to do with them. The > first would require giving these two names their own bytecodes, the > second makes them simple local variables the same as the ones in the > method headers. The latter also allows them to be changed by the > method, which is probably not the world's best programming practice > although it's possible now. That's not what I asked. Both of those options are storing the instance within a stack frame, once it's been called. I'm asking how you would remember the instance during the interval from the time when the method is accessed until when it has been called. In the code above, the method is accessed just before it is stored in the dictionary. That is when the method wrapper is currently created, and the instance is available. It is not called until much later, possibly not even within the same function. How would you remember the instance over that period without wrapping the function? Cheers, Ian