Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!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; '3.2': 0.05; 'attributes': 0.05; 'instance': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '>>>>': 0.09; 'convention,': 0.09; 'parameter.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrong,': 0.09; 'def': 0.15; 'argument': 0.15; 'case.': 0.15; 'class,': 0.15; 'method.': 0.15; "subject:' ": 0.15; '(is': 0.16; 'did.': 0.16; 'reedy': 0.16; 'translates': 0.16; 'val': 0.16; 'wrote:': 0.16; 'wrap': 0.18; 'jan': 0.19; 'seems': 0.20; 'header:In-Reply- To:1': 0.22; 'dictionary': 0.23; 'mask': 0.23; 'pm,': 0.24; 'code.': 0.26; 'function': 0.27; 'somebody': 0.28; 'subject:need': 0.28; 'bit': 0.28; 'bound': 0.29; 'print': 0.29; 'example': 0.30; 'confused': 0.30; 'typeerror:': 0.30; 'class': 0.30; 'equivalent': 0.31; 'subject:?': 0.31; 'list': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'someone': 0.34; 'header:User-Agent:1': 0.34; 'right,': 0.34; 'header:X-Complaints-To:1': 0.35; 'object': 0.35; 'explain': 0.36; 'but': 0.37; 'received:org': 0.38; 'some': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; 'hope': 0.61; 'double': 0.61; 'special': 0.67; 'why?': 0.73; 'self.value': 0.84; 'subject:always': 0.84; 'subject:class': 0.84; 'subject:self': 0.84; 'terminology': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why do class methods always need 'self' as the first parameter? Date: Wed, 31 Aug 2011 18:40:26 -0400 References: <0dc26f12-2541-4d41-8678-4fa53f347acf@g9g2000yqb.googlegroups.com> <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net> 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314830484 news.xs4all.nl 2494 [2001:888:2000:d::a6]:43543 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12531 On 8/31/2011 1:12 PM, Prasad, Ramit wrote: >> def double(obj): return 2*obj.value >> >> class C: >> def __init__(self, val): >> self.value = val >> >> c = C(3) >> C.double = double >> c.doub = double >> # not c.double as that would mask access to C.double in c.double() >> print(double(c), >> C.double(c), c.double(), c.doub(c)) Above is 3.2 code. To be exactly equivalent with 2.x, you need class C(object): > Sorry if I get some of the following terminology wrong, I get a bit > confused on Python terms. I hope the following is still coherent. (Is > there a dictionary of Python terminology?) > Given the above example I get this >>>> print c.double(c) > TypeError: double() takes exactly 1 argument (2 given) Right, because c.double() translates to C.double(c), and c.double(x) translates to C.double(c,x), which is not valid. >>>> print c.doub(c) > 6 > > It seems to me that if I add a function to the list of class > attributes it will automatically wrap with "self" When accessed via an instance of the class, the instance is automagically added as the first argument to be bound to the first parameter. The name 'self' is a convention, not a requirement. > but adding it to > the object directly will not wrap the function as a method. Can > somebody explain why? Someone else did. Not wrapping is normal, wrapping is a special case. -- Terry Jan Reedy