A funnily inconsistent behavior of int and float

Mark Dickinson dickinsm at gmail.com
Mon Apr 7 21:53:22 CEST 2008


On Apr 7, 3:15 pm, "Terry Reedy" <tjre... at udel.edu> wrote:
>
> My suggestions:
> 1. Change signature to: int([number | string[, radix]).
> This makes it clear that radix can only follow a string without having to
> say so in the text.
>
> 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.
>
> After looking at any comments here, I will consider submitting these to the
> tracker.
>
> Terry Jan Reedy

Looks good! The description should probably also mention the optional
'0b', '0o' or '0x' (or '0B', '0O', '0X') allowed between the sign and
the digits (or before the digits in the case of a missing sign) when
base=2, base=8 or base=16.

The only base 0 versus base 10 difference I could find was the
following:

>>> int('033', 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 0: '033'
[38720 refs]
>>> int('033')
33

Mark



More information about the Python-list mailing list