A funnily inconsistent behavior of int and float
Terry Reedy
tjreedy at udel.edu
Mon Apr 7 15:15:29 EDT 2008
"Mark Dickinson" <dickinsm at gmail.com> wrote in message
news:1d11875a-470a-489f-910a-b420a145eb61 at a1g2000hsb.googlegroups.com...
| On Apr 7, 6:43 am, "Colin J. Williams" <fn... at ncf.ca> wrote:
| > This is good but the documentation for
| > 3.0 is missing the syntax documentation
| > from 2.5
|
| Is
|
|
http://docs.python.org/dev/3.0/reference/lexical_analysis.html#integer-literals
|
| the documentation that you're looking for?
|
| But it seems to me that Lie's original point isn't really
| about integer *literals* anyway---it's about the behaviour
| of the built-in int() function when applied to a string. So
|
| http://docs.python.org/dev/3.0/library/functions.html#int
|
| is probably the appropriate place in the documentation. And
| I agree that it could be made clearer exactly what strings are
| acceptable here.
Agreed.
It says "If radix is zero, the interpretation is the same as for integer
literals."
But integer literals are unsigned. Is radix 0 any different from the
default of radix 10?
It also says "If the argument is a string, it must contain a possibly
signed number of arbitrary size, possibly embedded in whitespace." But
only integers, not 'numbers' as some would understand that, are accepted.
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
More information about the Python-list
mailing list