Delegation in Python

Brian Gladman noone at nowhere.net
Sat Jan 24 19:18:50 EST 2015


On 24/01/2015 23:43, Chris Angelico wrote:
> On Sun, Jan 25, 2015 at 10:38 AM, Brian Gladman <noone at nowhere.net> wrote:
>> On 24/01/2015 23:22, Chris Angelico wrote:
>>> class RF(Fraction):
>>>     def is_integer(self):
>>>        return self.numerator % self.denominator == 0
>>
>> Thanks for your help on this.  I must admit that nowhere in a lot of
>> searching did I find that delegation is achieved by doing nothing!
> 
> Hehe :) If you want a technical look at it, what you have is the
> fundamental nature of subclassing: you derive from another class, and
> your class is identical to that with certain exceptions (in your case,
> an additional method). Python's way of handling that is called the
> Method Resolution Order or MRO, and you can find info on it on the
> web, eg:
> 
> http://python-history.blogspot.com/2010/06/method-resolution-order.html

Thanks for the further background.

Is there a way of doing delegation rather than sub-classing?

That is, can I create a class (say RF) that passes some of its methods
to Fraction for implementation but always returns an RF?

   Brian




More information about the Python-list mailing list