Since the octal prefix is now 0o
in Python 3 it's not legal to write 0777
any more. Okay.
So why is it legal to write 00
which evaluates properly to 0
whereas other digits trigger a syntax error?
>>> 01
...
File "<interactive input>", line 1
01
^
SyntaxError: invalid token
>>>
>>> 00
0
If one takes a look at the Lexical Analysis (Integer Literal Section) page:
integer ::= decinteger | bininteger | octinteger | hexinteger
decinteger ::= nonzerodigit (["_"] digit)* | "0"+(["_"] "0")*
...
So that means that a decinteger
either begins with a nonzero digit (followed by all possible digits and optionally underscores), or is a sequence of zeros with optionally underscores (which maps to zero).
The documentation furthermore states that:
Note that leading zeros in a non-zero decimal number are not allowed.
So it means they make an exception for zero (in all documentation for python-3.3 one can find there): you can write zero as a sequence of zeros. My guess is that of course they have to include "0"
(how else would you specify zero as a decinteger
?), so why not allow more zeros in that case, regardless of the number system, 000
is and stays zero. They probably do not want to allow 01
as a decinteger
to prevent accidentally running python-2.x code and thus obtaining totally different results.
Finally note that the underscores are only part of that specification since python-3.6: in the specifications for 3.5 they are not mentioned in the grammar.
In python-2.7 the documentation specifies a zero followed by other digits (also other zeros as an octinteger
:
integer ::= decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::= nonzerodigit digit* | "0"
octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+