PEP0238 lament

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Tue Jul 24 02:30:57 EDT 2001


23 Jul 2001 22:17:48 GMT, Martijn Faassen <m.faassen at vet.uu.nl> pisze:

> Huh? Which contexts are that? ints are converted to floats if you use
> them together with a float, yes. int + float results in a float answer,
> just like int/float does; why do you claim / is an exception in this? 

In all arithmetic operators and functions except '/' I can use an
int in place of a float and I will get equal answer, possibly with
a different type.

>> In languages where ints are implicitly converted to strings, addition
>> and concatenation are typically different operators. Like in Perl:
>> '+' and '.'.
> 
> And in Perl "1" + "foo" is 1. Hardly ideal as an example of separate
> operators. :)

A good example when the meaning depends on operators rather than types.
Types are unified, operators are separated.

In fact 1 and "1" (and results obtained by addition vs. concatenation)
are treated differently in Perl internally. Perl ints, floats and
strings are not kept in the same type. They only convert between one
another when requested by the context.

This is similar to int and float in Python. Well, they do have
different types visible to the programmer, but the distinction is
blurred by implicit conversions.

>> Python does treat ints and floats as different parts of the same
>> concept by letting them compare equal and convert as needed. So real
>> division and integer division should be distinguished by the operator.
> 
> Couldn't you make this argument for float multiplication and integer
> multiplication as well though?

No, because they do give the same answer, so I don't have to care
whether I have an int or float if I multiply it. If a is an int,
then float(a*b) == float(a)*b (and if it's not ==, it's very close,
such that it's hard to tell which imprecise answer is less precise).

Division is different.

> Ocaml has separate *. for float multiplication, for instance.

Because it doesn't have overloading nor implicit conversions at
all: it can't use the same operation for types without a common
unifier. Now it has an experimental overloading mechanism which will
be used for arithmetic.

> It does raise the question if polluting so much old code with this
> construct (and we can't change code samples) is worth having a
> slightly easier to teach language..

old_div is the last resort. The proper fix is to either leave '/'
or change it to div, which IMHO makes code more readable, not less.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list