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.072 X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'attribute': 0.07; 'attribute.': 0.09; 'def': 0.15; "'mylist'": 0.16; 'name)': 0.16; 'name):': 0.16; 'mon,': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'thanks,': 0.18; 'defined': 0.19; 'seems': 0.20; 'suggest': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; "shouldn't": 0.23; 'aug': 0.24; 'sender:addr:gmail.com': 0.25; "i'm": 0.27; 'raise': 0.28; 'looks': 0.29; 'example': 0.30; '(maybe': 0.30; 'thanks': 0.30; 'class': 0.30; "didn't": 0.31; 'seem': 0.31; 'certainly': 0.32; 'represents': 0.32; "isn't": 0.33; 'to:addr:python-list': 0.33; 'however,': 0.34; '...': 0.34; 'like:': 0.34; 'object': 0.35; 'charset:us-ascii': 0.36; 'beginning': 0.36; 'but': 0.37; 'something': 0.37; 'steven': 0.38; 'received:google.com': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.39; 'called': 0.40; 'subject:from': 0.40; "it's": 0.40; 'header :Message-Id:1': 0.61; 'john': 0.62; 'back': 0.62; 'fall': 0.64; 'super': 0.64; 'subject:without': 0.67; 'special': 0.67; 'doing.': 0.73; '(e.g.:': 0.84; 'delegation': 0.84; 'subject:class': 0.84; 'received:home': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:date:from:to:subject:message-id:in-reply-to:references :x-mailer:mime-version:content-type:content-transfer-encoding; bh=4IP70mx7VD9L1hnWjaDDc+x4ubx4atQfA26+Ox+CGOM=; b=pJv8RgcjVxhS6RFpHnJzLBvBnE5n+3hw2Z4a7Ho0FR40935dx5WS2Wl2SAPWmDBFx4 hd+Obe1oZwc/iuyve2GWnZGdxuG8eCSzHj3Ng01appOtkkCdg6CvRTkouYVKb067dgFx LP6C01MAaWtCFTuj//BvDXswBOftjx2yAOqDA= Sender: "John O'Hagan" Date: Mon, 22 Aug 2011 23:08:50 +1000 From: John O'Hagan To: python-list@python.org Subject: Re: Adding modified methods from another class without subclassing In-Reply-To: <4e51e8c9$0$29981$c3e8da3$5496439d@news.astraweb.com> References: <4e51e8c9$0$29981$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Sylpheed 3.2.0beta1 (GTK+ 2.24.4; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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: 1314018545 news.xs4all.nl 23940 [2001:888:2000:d::a6]:51180 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12025 On Mon, 22 Aug 2011 15:27:36 +1000 Steven D'Aprano wrote: > On Mon, 22 Aug 2011 03:04 pm John O'Hagan wrote: > > > The "pitches" attribute represents the instances and as such I found > > myself adding a lot of methods like: > > > > def __getitem__(self, index): > > return self.pitches[index] > > > > def __len__(self): > > return len(self.pitches) > > > Looks like a call for (semi-)automatic delegation! > > Try something like this: > > > # Untested > class MySeq(object): > methods_to_delegate = ('__getitem__', '__len__', ...) > pitches = ... # make sure pitches is defined > def __getattr__(self, name): > if name in self.__class__.methods_to_delegate: > return getattr(self.pitches, name) > return super(MySeq, object).__getattr__(self, name) > # will likely raise AttributeError Thanks, this looks promising. I didn't know about __getattr__ or delegation. This example doesn't seem to work as is for special methods beginning with "__" (e.g.: "TypeError: object of type 'MyList' has no len()"). It seems that __getattr__ is not called for special methods. Also, it doesn't immediately suggest to me a way of modifying method calls (maybe __setattr__?). But it's certainly a neater way to get methods to operate on the attribute. I'm looking into it, and delegation generally. However, I don't understand what the super call is doing. If the method isn't delegated, shouldn't it just fall back to getattr(self, name)? Thanks and regards, John