Delegation in Python

Chris Angelico rosuav at gmail.com
Sun Jan 25 03:07:30 EST 2015


On Sun, Jan 25, 2015 at 6:49 PM, Brian Gladman <noone at 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


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /