[Python-3000] Fwd: Re: Fwd: Re: Octal

Guido van Rossum guido at python.org
Thu Mar 15 00:08:03 CET 2007


int('0012') should still return 12, no doubt about it, since the
default base is 10 and this is currently supported. Some business
users need to easily parse numbers output by Cobol. So should
int('0012', 10), which is really the same case. For int('0012', 0),
(the auto-base option) I'm not sure, as this may well be used to parse
e.g. command-line options by folks who don't know or care about Python
syntax. Of course, it ought to be sufficient to handle this in
optparse; I'm sure a simple regexp can distinguish between all the
different cases.

(Of course the auto-base option should support 0x, 0t and 0b prefixes;
it's just the combination of auto-base with decimal leading zeros that
might be confusing.)

I agree with Thomas that allowing the literal 0012 in a program to
return the value 10 would trip up old-timers too easily. I don't
expect Cobol code to be producing a lot of Python souce code, and it's
currently not supported (well, it is, but with a different meaning) so
I think it's OK to say that leading zeros are out (except for 0
itself, and 0x/0b/0t).

IOW, the only thing I'm unsure about is whether int('0012', 0) should
return 10 or complain like eval('0012') would. Perhaps it's best to
say that int('0012', 0) is a safer way to parse Python int literals,
and that would make it an error.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list