Delegation in Python

Chris Angelico rosuav at gmail.com
Sat Jan 24 18:43:33 EST 2015


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

Enjoy!

ChrisA



More information about the Python-list mailing list