Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84560

Re: Delegation in Python

References (2 earlier) <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 2015-01-25 19:07 +1100
Subject Re: Delegation in Python
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.18130.1422173260.18130.python-list@python.org> (permalink)

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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