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?
My guess is that since
012is no longer an octal literal constant in python3.x, they disallowed the012syntax to avoid strange backward compatibility bugs. Consider your python2.x script which using octal literal constants:Then you port it to python 3 and it still works — It just gives you
a = 25instead ofa = 21as you expected previously (decimal). Have fun tracking down that bug.