PEP239 (Rational Numbers) Reference Implementation and new issues

Alexander Schmolck a.schmolck at gmx.net
Thu Oct 3 15:07:35 CEST 2002


python-pep at ccraig.org writes:
> I just uploaded a reference implementation of how rationals might look
> in Python as patch 617779 [1].  I do have some new issues for
> discussion that I'd like to get some comments on before I change the
> PEP.

Is there any serious reason not to have a rational class that partions into
int[eger]s and rat[io]s (a la common lisp) [*]? 

Then ``>>> 1`` would be an int (and also a rational, because int is a subclass
of rational). ``2/2`` would also be the int 1, because rationals that are
expressible as ints are canonicalized to int.``>>> 1 / 2`` on the other hand
would be the rat[io] 1/2.


This solution would seem to me to have the advantage that it is 

a) perfectly intuitive to newbies
b) "mathematically correct" and elegant
c) exactness preserving
d) doesn't involve any more or less arbitrary design decisions (e.g.  indexing
   and similar issues raised in the pep)
e) entails a convenient and natural notation for ratios: ``1/3`` (not 1/3r or
   rational(1,3) or whatever)

alex


[*] more precisely, something like this:

                rational
                /     \
              integer  ratio
              /     \
            bignum   int



More information about the Python-list mailing list