int('2.1') does not work while int(float('2.1')) does

Mel Wilson mwilson at the-wire.com
Tue Apr 6 08:20:44 EDT 2004


In article <slrnc74a1l.bqf.joe at gate.notcharles.ca>,
Joe Mason <joe at notcharles.ca> wrote:
>In article <40722555.66C70D40 at alcyone.com>, Erik Max Francis wrote:
>> There's a fundamental difference between an int and a float.  If the
>> string you're trying to convert looks like a float, that means it
>> doesn't look like an int.
>>
>> With your first example of '2.1', what should it mean?  Should it
>> truncate, round to negative infinity round to positive infinity, round
>> to zero, what?  Python can't guess for you, so it shouldn't try.  Thus
>> it's an error.
>
>Why can it make this guess for "int(2.1)", then?  It's got a rule for
>converting floats to ints - why not use it here?

   I see it more as a programmer interface issue.  There are
kinds of operation that can suggest (especially in a
dynamically-typed language) that your program has gotten
away from you.  Python creator(s?) could have drawn this
particular line to this side or that, but in historical fact
they drew it here.  Perhaps on the principle of one
conversion at a time, please.

   You could argue that unit testing, the universal solvent,
would clean up this too.

        Regards.        Mel.



More information about the Python-list mailing list