SUMMARY
=========

It was once a good idea, for Python 3 to forbid leading zeroes in integer
literals. Since then circumstances have changed. Perhaps it's now, or soon
will be, a good time to permit this.

A SURPRISE
==========

I was surprised by:
>>> 0
0
>>> 00
0
>>> 000
0
>>> 001
SyntaxError: invalid token

Compare this to:
>>> int('0'), int('00'), int('000'), int('001')
(0, 0, 0, 1)

And also:
>>> 001.0
1.0


A NEWBY GOTCHA
================

A standard way of writing today's date is 2020-02-06. Let's try this in
Python, first with Christmas Day, and then for today:

>>> datetime.date(2020, 12, 25)
datetime.date(2020, 12, 25)
>>> datetime.date(2020, 02, 06)
SyntaxError

EXPLANATION
============

So what's happening? Briefly, in Python 2.7  we have two forms with the
same meaning

>>> 01, 010
(1, 8)
>>> 0o1, 0o10
(1, 8)

or in other words 010 is an OCTAL integer literal. In Python3 this was
removed, and instead we have only:

>>> 0o1, 0o10
(1, 8)
>>>
>>> 0O1, 0O10
(1, 8)

In some fonts, capital O and digit 0 are very similar. So using capital O
for OCTAL is probably a bad thing to do.

CONCLUSIONS
=============

Here's my view of the situation.

1. It was good to remove from Python3 the octal trap for 010. (In the
beginning, most Python users already knew C. I doubt that's true now. And
explicit is better than implicit.)

2. Once we've done that it made sense not to reuse 010 as a form for
decimal numbers. (We don't want code that is valid for both Python 2 and
Python 3, but with different meaning. Particularly for something so basic
as a integer literal. Errors should never pass silently.)

3. Python2 is now obsolete. Perhaps now, or sometime in the next year or
two, it makes sense to enable leading zeros in integer literals.

4. This would allow us to remove the newby trap, by providing:

>>> datetime.date(2020, 02, 06)
datetime.date(2020, 02, 06)

Notice I've changed the repr of today's date!

REFERENCES
============

Here's a couple of stackoverflow posts:
https://stackoverflow.com/questions/36386346/syntaxerror-invalid-token
https://stackoverflow.com/questions/50290476/why-python-shows-invalid-token-for-datetime2018-01-01-10-08-00

Here's a thread that brought this syntax error to my attention.
https://mail.python.org/archives/list/python-ideas@python.org/message/2NCIAUM4O55BWHHO7RMZNRPDUYM3JWYT/

-- 
Jonathan
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7IKEXSMVVDOTLHA7CNI7MIOBK4EU2ZSG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to