[Python-ideas] Disallow "00000" as a synonym for "0"
Steven D'Aprano
steve at pearwood.info
Mon Jul 20 04:01:55 CEST 2015
On Mon, Jul 20, 2015 at 10:38:02AM +1000, Nick Coghlan wrote:
> Exactly - the special case here is *dis*allowing leading zeroes for
> non-zero integer literals,
That's a ... different ... definition of "special case". You're saying
that, out of the literally infinite number of possible ints one might
attempt to write, *all of them except zero* are the special case, while
zero itself is the non-special case. O-kay.
I get the argument that allowing people to write 000000000 when they
want a int zero is harmless, and the code churn required to prevent that
outweighs any benefit gained. The task I created on the tracker has been
closed as a "won't fix" or "rejected" (I forget which one), and I'm not
going to argue with that. But I did find your perspective above funny
enough that I had to reply.
But for the record, and this will be my last word on the subject:
> since we can't know if they're supposed to be
> octal values or not, or if the "b" or "x" was left out of a binary or hex
> literal that has been padded out to a fixed number of bits, or if the
> decimal point was left out of a floating point literal.
Right. So if you see somebody has written 00 as a literal in Python 3,
it's *more likely to be an error than deliberate*. E.g. we can align
octal or hex numbers using a fixed width string with leading zeroes
where needed:
nums = [0x00001234,
0x000000A3,
0x00000005,
0x000027F3,
0x00000000, # this is okay
00000000, # breaks the alignment
0000000000, # aligned but where's the X?
0x0000C21D,
]
but if you see a plain, unprefixed 0 mixed in there, that smells of an
possible error. Even if it isn't an error, to me it hints of an error
enough that I'd want to question the code author's intention. "Did you
really mean zero, or is that a typo?"
Regardless of everything else, to me this is an aesthetic question.
Allowing 00 when 01, 02, 03, ... are (rightly!) forbidden feels ugly and
a wart. But until and unless somebody actually gets bitten by this in
real code, and a typo hides in plain view disguised as zero, I can't
honestly say it is outrightly harmful.
--
Steve
More information about the Python-ideas
mailing list