How about adding rational fraction to Python?

Paul Rubin http
Sun Mar 2 08:02:13 CET 2008


Lie <Lie.1296 at gmail.com> writes:
> So basically they refused to satisfy everything that is still possible
> individually but would conflict if done together. 

I can't understand that.

> x = 1
> a = x + 1    << decides it's an int

No, so far a and x are both Num (indeterminate)

> b = x + 1.0  << error? or redefine to be float?

This determines that a, b, and x are all floats.  It's not "redefined"
since the types were unknown prior to this.  

Actually, I'm slightly wrong, 1.0 is not a float, it's a "Fractional"
which is a narrower class than Num but it could still be Float, Double,
or Rational.  Nums support addition, subtraction, multiplication, but
not necessarily division.  So int/int is an error.  Fractionals support
division.

> c = x + 1    << would this cause error while it worked in line 2?

No, c is also a float (actually Fractional)

> A slightly obfuscated example:
> l = [1, 1.0, 1]

This is the same as l = [1.0, 1.0, 1.0].  In Haskell, all elements
of a list must have the same type, so the 1.0 determines that l is
a list of fractionals.

> x = 1
> for n in l:
>   c = x + n

Haskell does not have loops, but if it did, all these values would be
fractionals.



More information about the Python-list mailing list