[Tutor] exceptions problem
Francesco Loffredo
fal at libero.it
Mon Sep 13 15:07:45 CEST 2010
First, *THANK YOU!* for your clear and thorough explaination, Steven.
On 13/09/2010 13.59, Steven D'Aprano wrote:
> On Mon, 13 Sep 2010 08:55:46 pm Francesco Loffredo wrote:
>> I don't like this rough behaviour of int(), spitting out an
>> exception if given a legitimate string representation of a float. Can
>> some of you Tutors explain me why it must be so?
>
> The int() function behaves as a constructor, producing an integer object
> from its argument. It has two jobs:
>
> (1) truncate (round-to-zero) numbers to a whole number by dropping any
> fraction part without rounding; and
>
> (2) convert strings to an integer.
>
>
> So int() can truncate all of these numbers:
>...
> Okay, that's numbers out of the way. Pretty straightforward.
>
> When it comes to strings, we're doing *conversions*, not truncation, so
> we need to decide what are the rules for converting strings. The usual
> rule is that, apart from a possible leading + or - sign, we expect a
> string of digits. Nobody expects int("two hundred and thirty-seven") to
> return 237. What digits are allowed? That depends on the base -- Python
> supports bases from 2 to 36:
>
>>>> int("123") # base 10 is the default
> 123
>>>> int("123ff", 16) # hexadecimal
> 74751
>>>> int("123ff", 36)
> 1777371
>
> There's also a "base 0", which uses the string's prefix to specify the
> base:
>
>>>> int("0x123ff", 0) # 0x... means a hex number
> 74751
>
>
> So what are we to make of a string like "1.95"? Obviously it's not an
> integer, and "." is not a valid digit. If you, the programmer, are
> reading in data from a file and are expecting *integers*, and somebody
> slipped in a decimal-point, that is just as troublesome as if they
> slipped in a semi-colon or the letter Z. Should "1.95" be truncated to
> 1, or rounded to 2? Should it remain a float? Should the dot be
> interpreted as a digit in some unusual base?
>
> Python refuses to guess what int("1.95") should mean, and it raises an
> error, just as it does for int("1+1") or int("two").
I understand this, but I don't quite agree. I mean, I don't think
there's anything to guess. There is no ambiguity in "3.72". As far as I
know, there is no base, between 2 and 36, in which "." represents a
valid digit. So it would be *very* easy to do what float() does, then
truncate as int() already does. Anyway, now that I know, I could always
use int(float(x))... even if I find it weird.
Francesco
-------------- next part --------------
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3130 - Data di rilascio: 09/12/10 08:34:00
More information about the Tutor
mailing list