Delegation in Python

Brian Gladman noone at nowhere.net
Sat Jan 24 17:57:00 EST 2015


I would appreciate advice on how to set up delgation in Python.

I am continuously implementing a function to test whether a Python
Fraction is an integer so I wanted to define a new class, based on
Fraction, that includes this new method.

But I am not clear on how to delegate from my new class to the existing
Fraction class.  This is what I have:

--------------------------
class RF(Fraction):

  def __new__(self, x, y):
    super().__new__(self, x, y)

  def is_integer(self):
    return self.numerator % self.denominator == 0

  def __getattr__(self, attr):
    return getattr(self, attr)
--------------------------

which doesn't work.

Any advice on how to do this would be much appreciated.

   Brian



More information about the Python-list mailing list