Delegation in Python
Brian Gladman
noone at nowhere.net
Sun Jan 25 02:43:59 EST 2015
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,
>>>> from fractions import Fraction as F
>>>> F(4, 2)
> Fraction(2, 1)
>>>> F(12, 3)
> Fraction(4, 1)
>
> the test is that the denominator is 1
Thanks Terry, I should have realised this but didn't.
>>>> F(12,3).denominator == 1
> True
>>>> F(12,5).denominator == 1
> False
>
> it may not be worth the bother to define a function,
>
> def fint(F): return F.denominator == 1
>
>> so I wanted to define a new class, based on
>> Fraction, that includes this new method.
>
> but if you do, there is little need to make it a method. You are not
> overriding an existing method or adding a new special method. The math
> module has float and int functions that have *not* been turned into
> methods. Ditto for cmath. I would just leave the function a function.
Yes, it seems so.
Brian
More information about the Python-list
mailing list