Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; '*args,': 0.09; 'derived': 0.09; 'method,': 0.09; 'operand': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'attributes,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance:': 0.16; 'magic': 0.16; 'operation,': 0.16; 'typeerror:': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'documented': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; '"",': 0.31; '25,': 0.31; 'file': 0.32; 'class': 0.32; 'languages': 0.32; 'quite': 0.32; '(most': 0.33; 'brian': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'etc': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'pm,': 0.38; 'recent': 0.39; 'how': 0.40; 'manually': 0.60; 'effective': 0.61; 'map': 0.64; 'more': 0.64; 'wish': 0.70; 'to,': 0.72; 'felt': 0.74; '2015': 0.84; 'type(s)': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=xg7GTGVi5p/kFGsWSHbUo5W+r4NQAgGkwg+m/zLVEMk=; b=nqiIFKqnWrlxIo2qhNRCnuq+uROgRZpcgWiyd3aVSdhI/VZdguxC0AfucmjiXnZIGs 67Kenww/u68s784xoEqlf2A1ZqGMoNte2nr5XUqneGrkU1y4o7iaqFviclYHRC2/8zZ0 jO4PtGvHJrWjBtd8RSV8H1GtjirRSfscrOhHqQ+060rEfZjR1/cMoqhv25wbsMxILg7Q aqb1xkGz70CyoZ75CGjvqKxfrC/ewCs7LYWTF7NYBdJ1NYaxGp1699bxEdhu1SqRSLwy fhWke1ehzsGuwvA5wJaiObeDNrI7M95cZXC7tOmojVYmf062WzQvV5XBLYHNJhzjt7mk 4i8Q== MIME-Version: 1.0 X-Received: by 10.50.66.179 with SMTP id g19mr10697776igt.34.1422173250990; Sun, 25 Jan 2015 00:07:30 -0800 (PST) In-Reply-To: References: <5ZKdnd6rbdZ3sVnJnZ2dnUVZ8mCdnZ2d@brightview.co.uk> Date: Sun, 25 Jan 2015 19:07:30 +1100 Subject: Re: Delegation in Python From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 1422173260 news.xs4all.nl 2938 [2001:888:2000:d::a6]:50623 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84560 On Sun, Jan 25, 2015 at 6:49 PM, Brian Gladman wrote: > Thanks, a part of this was a wish to understand how to map what I can do > in other languages into Python. I felt that it might just be possible > in Python to avoid having to wrap all the methods of the base class in > the derived class. But it seems that __getattr__ etc are not quite as > magic as I hoped. They do exactly what they're documented to, nothing more and nothing less :) It's certainly possible to use them to hook into missing attributes, for instance: >>> class RF: def __init__(self, *args, **kw): self._frac = Fraction(*args, **kw) def __getattr__(self, attr): return getattr(self._frac, attr) def __repr__(self): return "RF(%d, %d)" % (self._frac.numerator, self._frac.denominator) def is_integer(self): return self._frac.denominator==1 >>> RF(1,4).numerator 1 But it doesn't work for everything: >>> RF(1,4)*2 Traceback (most recent call last): File "", line 1, in RF(1,4)*2 TypeError: unsupported operand type(s) for *: 'RF' and 'int' The only solution would be to go through every operation that you care about, and manually hook them. Something like this: def __mul__(self, other): result = self._frac * other if isinstance(result, Fraction): return RF(result.numerator, result.denominator) return result >>> RF(1,4)*2 RF(1, 2) And do that for every other operation, method, etc. Tedious, but can be effective if you need it. ChrisA