Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84560
| 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 | <rosuav@gmail.com> |
| 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 | <KYednbizIP5rAlnJnZ2dnUVZ7sidnZ2d@brightview.co.uk> |
| References | <FeCdnXdsTpunvlnJnZ2dnUVZ8judnZ2d@brightview.co.uk> <mailman.18106.1422141738.18130.python-list@python.org> <5ZKdnd6rbdZ3sVnJnZ2dnUVZ8mCdnZ2d@brightview.co.uk> <mailman.18109.1422143020.18130.python-list@python.org> <K8KdncuoKPL1q1nJnZ2dnUVZ8h2dnZ2d@brightview.co.uk> <mailman.18116.1422145722.18130.python-list@python.org> <KYednbizIP5rAlnJnZ2dnUVZ7sidnZ2d@brightview.co.uk> |
| Date | Sun, 25 Jan 2015 19:07:30 +1100 |
| Subject | Re: Delegation in Python |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18130.1422173260.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Sun, Jan 25, 2015 at 6:49 PM, Brian Gladman <noone@nowhere.net> 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 "<pyshell#37>", line 1, in <module>
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-24 22:57 +0000
Re: Delegation in Python Chris Angelico <rosuav@gmail.com> - 2015-01-25 10:22 +1100
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-24 23:38 +0000
Re: Delegation in Python Chris Angelico <rosuav@gmail.com> - 2015-01-25 10:43 +1100
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-25 00:18 +0000
Re: Delegation in Python Chris Angelico <rosuav@gmail.com> - 2015-01-25 11:28 +1100
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-25 07:49 +0000
Re: Delegation in Python Chris Angelico <rosuav@gmail.com> - 2015-01-25 19:07 +1100
Re: Delegation in Python Gary Herron <gherron@digipen.edu> - 2015-01-24 15:47 -0800
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-24 23:58 +0000
Re: Delegation in Python Gary Herron <gherron@digipen.edu> - 2015-01-24 15:41 -0800
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-24 23:52 +0000
Re: Delegation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-24 23:59 +0000
Re: Delegation in Python Chris Angelico <rosuav@gmail.com> - 2015-01-25 11:07 +1100
Re: Delegation in Python Terry Reedy <tjreedy@udel.edu> - 2015-01-24 20:31 -0500
Re: Delegation in Python Brian Gladman <noone@nowhere.net> - 2015-01-25 07:43 +0000
csiph-web