Conflicting needs for __init__ method

Mark Dickinson dickinsm at gmail.com
Mon Jan 15 17:35:37 CET 2007



On Jan 14, 10:43 pm, Steven D'Aprano
<s... at REMOVEME.cybersource.com.au> wrote:
> On Sun, 14 Jan 2007 15:32:35 -0800, dickinsm wrote:
> > (You could include the normalization in __init__, but that's wasteful
> Is it really? Have you measured it or are you guessing? Is it more or less
> wasteful than any other solution?

Just guessing :).  But when summing the reciprocals of the first 2000
positive integers, for example, with:

sum((Rational(1, n) for n in range(1, 2001)), Rational(0))

the profile module tells me that the whole calculation takes 8.537
seconds, 8.142 of which are spent in my gcd() function.  So it seemed
sensible to eliminate unnecessary calls to gcd() when there's an easy
way to do so.

>     def __copy__(self):
>         cls = self.__class__
>         obj = cls.__new__(cls)
>         obj.numerator = self.numerator
>         obj.denominator = self.denominator
>         return obj

Thank you for this.

Mark




More information about the Python-list mailing list