A funnily inconsistent behavior of int and float

Lie Lie.1296 at gmail.com
Mon Apr 7 16:59:36 EDT 2008


On Apr 8, 2:15 am, "Terry Reedy" <tjre... at udel.edu> wrote:
(snip)
> 2. Replace text with:
> Convert a number or string to an integer.  If no arguments are given,
> return 0.  If a number is given, return number.__int__().  Conversion of
> floating point numbers to integers truncates towards zero.  A string must
> be a base-radix integer literal optionally preceded by '+' or '-' (with no
> space in between) and optionally surrounded by whitespace.  A base-n
> literal consists of the digits 0 to n-1, with 'a' to 'z' (or 'A' to 'Z')
> having values 10 to 35.  The default radix is 10. The allowed values are 0
> and 2-36, with 0 the same as 10.
>
> If 0 is not the same as 10, the last would have to be changed, but I could
> not detect any difference in a quick test.

One thing though, I think it should say "may be surrounded by
whitespace" as opposed to "optionally surrounded by whitespace".

On Apr 7, 1:55 am, Grant Edwards <gra... at visi.com> wrote:
> On 2008-04-06, Lie <Lie.1... at gmail.com> wrote:
>
> > I've noticed some oddly inconsistent behavior with int and float:
>
> > Python 2.5.1 (r251:54863, Mar  7 2008, 03:39:23)
> > [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> >>>> int('-          345')
> > -345
>
> > works,
>
> IMO, it oughtn't.
>
> >>>> float('-       345.083')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > ValueError: invalid literal for float(): -       345.083
>
> That's the behavior I'd expect.

Sorry to confuse you, by works I mean that the interpreter doesn't
complain at all, I didn't mean that it works as it should be.



More information about the Python-list mailing list