[Tutor] Why does 01 give a syntax error, but 00 (or 00...0) does not?
Ben Finney
ben+python at benfinney.id.au
Sat Mar 5 18:33:38 EST 2016
boB Stepp <robertvstepp at gmail.com> writes:
> >>> 01
> File "<stdin>", line 1
> 01
> ^
> SyntaxError: invalid token
As you later explain, this is because in Python 3 the obsolete “leading
0 for octal notation” is invalid syntax. (Superseded by “leading ‘0o’ for
octal notation”).
> >>> 00
> 0
> >>> -00
> 0
> >>> 000
> 0
>
> Why do zeros not give a syntax error, but other numbers with a leading
> zero do give a syntax error?
Hmm. That is a confusing inconsistency, I agree. It is deliberate, and
explicitly documented:
Note that leading zeros in a non-zero decimal number are not
allowed. This is for disambiguation with C-style octal literals,
which Python used before version 3.0.
<URL:https://docs.python.org/3/reference/lexical_analysis.html#integer-literals>
which does not explain why “non-zero decimal number” was chosen, rather
than simply any decimal number.
> But then why is 00...0 valid, that is, does not give a syntax error?
At one level, the answer is: because the language reference declares
those rules for the syntax.
At another level, the answer is: I don't know why that decision was
made, and on the face of it I disagree.
--
\ “But it is permissible to make a judgment after you have |
`\ examined the evidence. In some circles it is even encouraged.” |
_o__) —Carl Sagan, _The Burden of Skepticism_, 1987 |
Ben Finney
More information about the Tutor
mailing list