Re: Delegation in Python

2015-01-25 Thread Chris Angelico
On Sun, Jan 25, 2015 at 6:49 PM, Brian Gladman 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. B

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
On 25/01/2015 00:28, Chris Angelico wrote: > On Sun, Jan 25, 2015 at 11:18 AM, Brian Gladman wrote: >> 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 R

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
On 25/01/2015 01:31, Terry Reedy wrote: > On 1/24/2015 5:57 PM, Brian Gladman wrote: >> 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 > > Since Fractions are reduced to lowest terms

Re: Delegation in Python

2015-01-24 Thread Terry Reedy
On 1/24/2015 5:57 PM, Brian Gladman wrote: 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 Since Fractions are reduced to lowest terms, >>> from fractions import Fraction as F >>> F(4, 2)

Re: Delegation in Python

2015-01-24 Thread Chris Angelico
On Sun, Jan 25, 2015 at 11:18 AM, Brian Gladman wrote: > 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? Hmm. The key here is that you want more than

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
On 24/01/2015 23:43, Chris Angelico wrote: > On Sun, Jan 25, 2015 at 10:38 AM, Brian Gladman 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 mu

Re: Delegation in Python

2015-01-24 Thread Chris Angelico
On Sun, Jan 25, 2015 at 10:59 AM, Mark Lawrence wrote: >> You can always "monkey-path" the Fraction class on the fly to add a new >> method to it. I think most would consider this a bad idea, but it does >> work. > > As regards this being a bad idea I'd suggest the latest score is > Practicality

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
On 24/01/2015 23:47, Gary Herron wrote: > On 01/24/2015 03:38 PM, Brian Gladman 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 n

Re: Delegation in Python

2015-01-24 Thread Mark Lawrence
On 24/01/2015 23:41, Gary Herron wrote: On 01/24/2015 03:22 PM, Chris Angelico wrote: On Sun, Jan 25, 2015 at 9:57 AM, Brian Gladman wrote: 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(Fractio

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
On 24/01/2015 23:41, Gary Herron wrote: [snip]> > You can always "monkey-path" the Fraction class on the fly to add a new > method to it. I think most would consider this a bad idea, but it does > work. > Try this: > from fractions import Fraction def is_integer(self): > ... return

Re: Delegation in Python

2015-01-24 Thread Gary Herron
On 01/24/2015 03:38 PM, Brian Gladman 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

Re: Delegation in Python

2015-01-24 Thread Chris Angelico
On Sun, Jan 25, 2015 at 10:38 AM, Brian Gladman 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

Re: Delegation in Python

2015-01-24 Thread Gary Herron
On 01/24/2015 03:22 PM, Chris Angelico wrote: On Sun, Jan 25, 2015 at 9:57 AM, Brian Gladman wrote: 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): sup

Re: Delegation in Python

2015-01-24 Thread Brian Gladman
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! Brian -

Re: Delegation in Python

2015-01-24 Thread Chris Angelico
On Sun, Jan 25, 2015 at 9:57 AM, Brian Gladman wrote: > 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