This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inconsistent leading zero treatment
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: later
Dependencies: Superseder:
Assigned To: Nosy List: Peter.Wentworth, eric.smith, ezio.melotti, mark.dickinson, nadeem.vawda, petri.lehtinen, r.david.murray, rhettinger, stutzbach
Priority: normal Keywords:

Created on 2011-05-20 04:19 by Peter.Wentworth, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (12)
msg136352 - (view) Author: Peter Wentworth (Peter.Wentworth) Date: 2011-05-20 04:19
In Python 3 we no longer have octal literals. The assignment
x = 0050  
gives an invalid token error.

But the assignment
y = int("0050") assigns the value 50 to y.

I would advocate consistency in the two situations, and prefer that leading zeros should be permitted on numbers.
msg136354 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-05-20 05:20
There's a good rationale against supporting leading zeros in integer literals in PEP 3127:

    http://www.python.org/dev/peps/pep-3127/#removal-of-old-octal-syntax
msg136355 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-20 07:08
I think this is something that should be brought up for wider discussion on the python-dev mailing list.  It may be that people are ready to allow those leading zeros for Python 3.3 or 3.4.
msg136363 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-20 11:53
Do you have any use case for x = 0050?
I don't any reason to adding leading 0s to a literal, but with int() the situation is different because you might get string with leading 0s from somewhere else.  Also note that the int() function is more flexible and there are other similar "inconsistencies":

>>> x = int('12')
>>> x
12
>>> x = 12
  File "<stdin>", line 1
    x = 12
             ^
SyntaxError: invalid character in identifier


I suggest closing this as "rejected".
msg136364 - (view) Author: Nadeem Vawda (nadeem.vawda) * (Python committer) Date: 2011-05-20 12:05
The behaviour of int() can be made consistent with the syntax for Python
integer literals by specifying a base of 0:

    >>> int("0050", 0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 0: '0050'

    >>> int("0o050", 0)
    40

    >>> int("0x050", 0)
    80

    >>> int("0b010", 0)
    2

As for changing the default behaviour, I think it would be a bad idea.
Having ``0050'' be an invalid token is perhaps jarring to people used to
C/C++/Java/etc., but at least attempts to use it fail loudly and
obviously. Having it be valid and yet mean something different could lead
to nasty surprises for the unsuspecting.
msg136367 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-20 12:09
Well, I'd personally like to see those leading zeros accepted at some point in Python's future, for the sake of cleanliness and consistency.  Not just consistency with int(), but also e.g. with float literals:

>>> 0050.
50.0
>>> 0050
  File "<stdin>", line 1
    0050
       ^
SyntaxError: invalid token

I'm just not sure whether we're at that point yet.

I don't find the PEP 3127 argument that we should protect people from subsequent confusion in other languages with 'leading zero implies octal' semantics at all convincing.

On the other hand, I do see the case for not having Python 3 silently behave differently from Python 2.

It seems worth a discussion, at least.
msg136368 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-20 12:19
If the leading 0s are valid for floats (and complex) literals then int literals are indeed inconsistent.
If we want consistency with the other literals, it is probably better allowing them for ints, because removing them from float/complex is not backward compatible, but on the other hand it might be better to wait a few more versions to avoid surprises for people porting 2.x code to 3.x.
msg136370 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-05-20 13:04
I don't buy the "confusion with other languages" argument. It's a different language. People know that.

I'd like to see leading zeros allowed for integer literals, but I don't feel strongly about it, so +0. I'd mainly use them for tables of numbers that I'd like to line up.

If using 2to3 or 3to2, they could deal with it. The only case it would cause you problems is code that you want to run unchanged in 2.x and 3.x.
msg136373 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-05-20 13:43
IMO 3.3 is definitely too soon.  3.4 may be too, depending on how many people are stuck on legacy systems using 2.7.
msg136390 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2011-05-20 18:26
-0 on making it valid ever, due to the different meaning in many other languages.

-1 on making it valid while many still use a version of python where it's valid with a different meaning.  Maybe for Python 4. ;-)
msg136477 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-21 20:17
It does look as though all the arguments were pretty thoroughly hashed out on the python-3000 list when this was first proposed.  See e.g., the thread starting at:

http://mail.python.org/pipermail/python-3000/2007-March/006262.html

and various follow-on threads.  I retract my suggestion that this should be discussed again on python-dev; it probably wouldn't achieve anything.

On the subject of other languages, I was rather hoping that more recent and enlightened languages would have done away with the leading zero implies octal business.  It looks as though C# has, at least, but I was a bit disappointed to see that Go still has leading-zero octal literals.  

For the old-school languages, I'd still expect that for most users this feature is more often a surprising gotcha than expected and useful.

http://stackoverflow.com/questions/565634/integer-with-leading-zeroes

Anyway, +0 for closing as 'rejected'.  (Or perhaps 'wont fix'.)
msg137646 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-06-04 15:04
Closing, based on feedback in the comments.  Maybe one day...
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56336
2011-06-04 15:04:34mark.dickinsonsetstatus: open -> closed
resolution: later
messages: + msg137646
2011-05-21 20:17:45mark.dickinsonsetmessages: + msg136477
2011-05-20 18:26:01stutzbachsetnosy: + stutzbach
messages: + msg136390
2011-05-20 13:43:05r.david.murraysetnosy: + r.david.murray
messages: + msg136373
2011-05-20 13:04:56eric.smithsetmessages: + msg136370
2011-05-20 12:19:51ezio.melottisetmessages: + msg136368
2011-05-20 12:09:42mark.dickinsonsetmessages: + msg136367
2011-05-20 12:05:46nadeem.vawdasetnosy: + nadeem.vawda
messages: + msg136364
2011-05-20 11:53:21ezio.melottisetnosy: + rhettinger, ezio.melotti
messages: + msg136363
2011-05-20 09:16:32eric.smithsetnosy: + eric.smith
2011-05-20 07:08:38mark.dickinsonsetnosy: + mark.dickinson
messages: + msg136355
2011-05-20 07:02:54mark.dickinsonsettype: compile error -> enhancement
versions: + Python 3.3, - Python 3.1, Python 3.2
2011-05-20 05:20:38petri.lehtinensetnosy: + petri.lehtinen

messages: + msg136354
versions: + Python 3.2
2011-05-20 04:19:43Peter.Wentworthcreate