Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'instance': 0.05; 'run-time': 0.05; 'compiler': 0.07; 'interpreter': 0.07; 'wrappers': 0.09; 'am,': 0.12; 'method.': 0.15; "subject:' ": 0.15; 'bound,': 0.16; 'somehow,': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'figure': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'sep': 0.23; 'code': 0.25; 'function': 0.27; 'subject:need': 0.28; 'thu,': 0.28; 'bound': 0.29; 'rid': 0.29; 'work:': 0.29; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'class': 0.30; 'ago': 0.31; 'subject:?': 0.31; 'received:209.85.161.46': 0.31; 'received:mail- fx0-f46.google.com': 0.31; 'does': 0.32; 'that,': 0.33; '...': 0.34; 'determine': 0.35; 'object': 0.35; 'received:209.85.161': 0.35; 'variables': 0.37; 'could': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'returned': 0.39; 'john': 0.62; 'marked': 0.64; 'special': 0.67; 'overhead.': 0.84; 'subject:always': 0.84; 'subject:class': 0.84; 'subject:self': 0.84; 'attendant': 0.91 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; bh=exklAWXKdzYPQHF6HyjB9l1JrWSA0qQlYT4Gtp3nKe0=; b=Gd/do62QBnSxye2jkE/mPUK70QKsLL1NokzuUP0/J1KKsDtioNrj4+S5/gXAMqfOHa fLAu+sfCi9sXNq+hviBYVGaEc9tqjdxpgf4u1wgIXDqcTvFWhX9CmlmOFny/ihIpXg/5 OjAIqdP3O/kr7DuELgMSTSac6c849+ZEhB7hc= MIME-Version: 1.0 In-Reply-To: <42e335a7-b872-4229-ae02-13d61b7fab35@w22g2000prj.googlegroups.com> References: <0dc26f12-2541-4d41-8678-4fa53f347acf@g9g2000yqb.googlegroups.com> <42e335a7-b872-4229-ae02-13d61b7fab35@w22g2000prj.googlegroups.com> From: Ian Kelly Date: Thu, 1 Sep 2011 08:26:53 -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 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314887244 news.xs4all.nl 2524 [2001:888:2000:d::a6]:59204 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12566 On Thu, Sep 1, 2011 at 6:45 AM, John Roth wrote: > I personally consider this to be a wart. Some time ago I did an > implementation analysis. The gist is that, if self and cls were made > special variables that returned the current instance and class > respectively, then the compiler could determine whether a function was > an instance or class method. If it then marked the code object > appropriately you could get rid of all of the wrappers and the > attendant run-time overhead. I don't see how you could get rid of the wrappers. Methods would still need to be bound, somehow, so that code like this will work: methods = {} for obj in objs: if obj.is_flagged: methods[obj.user_id] = obj.do_work else: methods[obj.user_id] = 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