22

I just executed the following program on my python interpreter:

>>> def mylife(x):
...     if x>0:
...             print(x)
...     else:
...             print(-x)
... 
>>> mylife(01)
File "<stdin>", line 1
mylife(01)
        ^
SyntaxError: invalid token
>>> mylife(1)
1
>>> mylife(-1)
1
>>> mylife(0)
0

Now, I have seen this but as the link says, the 0 for octal does not work any more in python (i.e. does not work in python3). But does that not mean that the the behaviour for numbers starting with 0 should be interpreted properly? Either in base-2 or in normal base-10 representation? Since it is not so, why does python behave like that? Is it an implementation issue? Or is it a semantic issue?

Community
  • 1
  • 1

2 Answers2

32

My guess is that since 012 is no longer an octal literal constant in python3.x, they disallowed the 012 syntax to avoid strange backward compatibility bugs. Consider your python2.x script which using octal literal constants:

a = 012 + 013

Then you port it to python 3 and it still works -- It just gives you a = 25 instead of a = 21 as you expected previously (decimal). Have fun tracking down that bug.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Yup. Seems like a good explanation, as possible design decision. May be after two years, when most of the stuff has been ported, then, I think, the feature should be allowed again. Thanks! –  Oct 22 '12 at 14:42
  • 18
    Reminds of the "Why do programmers confuse Christmas and Halloween" - "Because dec(25) is oct(31)" ;P – Jon Clements Jan 28 '13 at 16:04
  • @JonClements -- I'd never seen that one before -- Quite clever -- I suppose that was probably mentioned in one of the CS classes I never took. – mgilson Jan 28 '13 at 16:05
  • @mgilson probably the good reason. Which explains why `000` is still valid: https://stackoverflow.com/questions/43071916/why-does-000-evaluate-to-0-in-python-3 – Jean-François Fabre Sep 01 '17 at 21:24
  • @mgilson with '0' it works , with '00' or '000' it fails giving that "invalid token" errro – SwissNavy Feb 16 '23 at 08:11
  • Octal was the first suspect for me too. However, in the code it's a str and quotes. In the DB it's quoted as well, where the numeric content can arise from? Also: the error is coming from psycopg2: `psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type json\\nLINE 3: ...d_on) = 2 AND (app_cache.some_field -> 2) IN (\\'00\\'...` . I have no control of what castings psycopg2 might possibly do inside... – SwissNavy Feb 16 '23 at 08:12
  • @SwissNavy -- yours is getting rejected by psycopg2 -- not python. Something interesting is happening when you are formatting that SQL query. That seems like a different root problem compared to _this_ question. – mgilson Feb 20 '23 at 14:00
15

From the Python 3 release notes http://docs.python.org/3.0/whatsnew/3.0.html#integers

Octal literals are no longer of the form 0720; use 0o720 instead.

The 'leading zero' syntax for octal literals in Python 2.x was a common gotcha:

Python 2.7.3
>>> 010
8

In Python 3.x it's a syntax error, as you've discovered:

Python 3.3.0
>>> 010
  File "<stdin>", line 1
    010
      ^
SyntaxError: invalid token

You can still convert from strings with leading zeros same as ever:

>>> int("010")
10
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465