How about adding rational fraction to Python?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Mar 1 11:23:46 EST 2008


On Fri, 29 Feb 2008 17:29:32 -0800, Lie wrote:

> On Feb 28, 10:00 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>> More examples:
>>
>>    x = 1
>>    y = len(s) + x
>>
>> => ok, decides that x is an int
>>
>>    x = 1
>>    y = x + 3.0
>>
>> => ok, decides that x is a float
>>
>>    x = 1
>>    y = x + 3.0
>>    z = len(s) + x
>>
>> => forbidden, x cannot be an int and float at the same time.
>>
>> > I am so glad you're not the designer of Python.
>>
>> This is how Haskell works and I don't notice much complaints about it.
> 
> Ok, that means the line "y = x + 3.0" have a side effect of "x =
> float(x)"? I think I would say that is an implicit behavior.

But the type of `x` must be specialized somehow.  `x` doesn't start as
`Int` or `Integer` but the very generic and AFAIK abstract type class `Num`.

After seeing the second line the compiler finds an implementation for `+`
and the type class `Fractional` for both operands and now thinks `x` must
be a `Fractional`, a subclass of `Num`.

Then comes the third line with `length` returning an `Int` and the
`Fractional` `x` but there is no implementation for a `+` function on
those types.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list