Leading 0's syntax error in datetime.date module (Python 3.6)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 10 07:34:28 EDT 2018


On Thu, 10 May 2018 12:43:33 +0200, Virgil Stokes wrote:

> Why does the datetime.date  module (both built-in and site-package) not
> accept leading 0's?


This has nothing to do with the datetime module. Syntax Error means it it 
prohibited by the language, not the module.

In Python 2, leading zeroes result in octal (base 8) numbers, which is 
harmless when there is a single digit below 8:

py> 05  # Python 2 octal
5


but mysterious if there is an 8 or 9 digit:

py> 09
  File "<stdin>", line 1
    09
     ^
SyntaxError: invalid token


and a source of errors when there are no 8 or 9 digits:

py> 0123  # pad one hundred twenty-three to four digits
83

So in Python 3, the syntax for octal digits was changed to use a 0o 
prefix, to match the 0x for hexadecimal and 0b for binary, and a bare 
leading 0 made an error.


-- 
Steve




More information about the Python-list mailing list