[Python-ideas] Disallow "00000" as a synonym for "0"

Chris Angelico rosuav at gmail.com
Sun Jul 19 18:37:44 CEST 2015

On Mon, Jul 20, 2015 at 2:22 AM, Ron Adam <ron3200 at gmail.com> wrote:
> The most common use of leading zero's is when numbers in strings are used
> and those numbers are sorted.  Without the leading zero's the sort order may
> not be in numerical order.
> The int class supports using leading zero's in strings, but not in literal
> form.  (Although it may use a C string to int function when parsing it.)
>>>> int("0000")
> 0
>>>> int("007")
> 7
>>>> int(0000)
> 0
>>>> int(007)
>   File "<stdin>", line 1
>     int(007)
>           ^
> SyntaxError: invalid token
>>>> int(007.0)
> 7
>>>> float("0001")
> 1.0

Yes, because int() can take an extra parameter to specify the base.
Source code can't.

> A social reason for this limitation is that a number of other languages do
> use a leading digit 0 to define octal numbers.  And this helps catch silent
> errors due to copying numbers directly in that case.  (Python uses "0o" and
> not just "0".)

Python 2 also supported C-style "0777" to mean 511, and it's all
through Unix software (eg "0777" is a common notation for a
world-writable directory's permission bits; since there are three bits
(r/w/x) for each of the three categories (u/g/o), it makes sense to
use octal). Supporting "0777" in source code and having it mean 777
decimal would be extremely confusing, which is why it's a straight-up
error in Python 3.


More information about the Python-ideas mailing list