Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.05; 'instance': 0.05; 'attribute': 0.07; 'method,': 0.07; 'python': 0.08; 'decorator': 0.09; 'descriptors': 0.09; "object's": 0.09; 'am,': 0.12; 'stored': 0.13; 'def': 0.15; 'method.': 0.15; "subject:' ": 0.15; '11:12': 0.16; 'above?': 0.16; 'definition,': 0.16; 'syntactic': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'meant': 0.17; 'wed,': 0.17; 'cheers,': 0.18; 'wrap': 0.18; 'to:2**1': 0.20; 'seems': 0.20; 'cc:2**0': 0.22; 'header:In-Reply- To:1': 0.22; 'aug': 0.24; 'function': 0.27; 'somebody': 0.28; 'subject:need': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; '(unless': 0.30; 'wraps': 0.30; 'class': 0.30; 'subject:?': 0.31; 'list': 0.32; 'generally': 0.32; 'does': 0.32; 'usually': 0.32; 'things': 0.34; 'function.': 0.34; 'pretty': 0.35; 'object': 0.35; 'explain': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'class.': 0.37; 'skip:i 30': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'i.e.': 0.39; 'data': 0.39; "it's": 0.40; 'more': 0.60; '31,': 0.64; 'why?': 0.73; 'viewed': 0.82; 'accessed.': 0.84; 'sorter': 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=M8ms+4H7DmFODEw2phBa99cPUOdVT324wNMh0OC2oWo=; b=MygBmmUsAWd4R3XGTe5n+Kr+UaHVQBniG8J457tbaZShhBzbd3JaXXVorfv9Gb8cu7 HMsyUDIb0FgeRTqFC5PzSNnFVaMMXfLrF+A95e3TnI8/jASdWGMV052qoqD1wXx7iQXG QrSF+G7koCFjByxoHNK2Id2aO/CvWMjYWc9vE= MIME-Version: 1.0 In-Reply-To: <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net> References: <0dc26f12-2541-4d41-8678-4fa53f347acf@g9g2000yqb.googlegroups.com> <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net> From: Ian Kelly Date: Wed, 31 Aug 2011 15:52:48 -0600 Subject: Re: Why do class methods always need 'self' as the first parameter? To: "Prasad, Ramit" 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314827599 news.xs4all.nl 2481 [2001:888:2000:d::a6]:50203 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12527 On Wed, Aug 31, 2011 at 11:12 AM, Prasad, Ramit wrote: > It seems to me that if I add a function to the list of class attributes i= t will automatically wrap with "self" but adding it to the object directly = will not wrap the function as a method. Can somebody explain why? I would h= ave thought that any function added to an object would be a method (unless = decorated as a class method). Because things stored on the class are generally viewed as part of the class definition, whereas things stored on an instance are generally viewed as data -- a function stored on an object instance is usually just meant to be a function. Consider the following code: class Sorter(object): def __init__(self, keyfunc): self.keyfunc =3D keyfunc def sort(self, item_list): item_list.sort(key=3Dself.keyfunc) sorter =3D Sorter(lambda x: x.id) sorter.sort(some_list_of_items) If adding keyfunc as an attribute to the object wrapped it up as a method, it would break, since the function is not expecting a "self" argument. More technically, because descriptors are only invoked when they're stored on the class. > Hmm, or does the decoration just tell Python not to turn an object's func= tion into a method? I.e. Is the decorator basically just the syntactic suga= r for doing the above? If you mean the staticmethod decorator, yes, it pretty much just wraps the function as a "staticmethod" instance to prevent it from being wrapped into an ordinary method when it's accessed. Cheers, Ian