2

Python seems to be able to accept leading zeros for any number except 08 or 09. For instance,

a = 04

works in the interpreter but

a = 08

returns

SyntaxError: invalid token

I'm using python 2.7.3 on OSX, and others have been able to duplicate the error. What gives?

user2171504
  • 75
  • 1
  • 5
  • related: http://stackoverflow.com/questions/13013638/python-cannot-handle-numbers-string-starting-with-0-why/13013678#13013678 – mgilson Mar 14 '13 at 20:18
  • Notice that `a = 044` "works", but probably doesn't do what you expected. – abarnert Mar 14 '13 at 20:24

2 Answers2

11

Numbers with a leading zero in them are interpreted as octal, where the digits 8 and 9 don't exist.

It's worse in Python 3, leading zeros are a syntax error no matter which digits you use. See What’s New In Python 3.0 under "New octal literals". Also PEP 3127.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 3
    In Python3, the syntax is now `0o123`. – Vincent Savard Mar 14 '13 at 20:18
  • 6
    I would argue that it's much *better* in Python 3, because then the kind of confusion the OP had doesn't even arise. – John Y Mar 14 '13 at 20:20
  • This is absolutely crazy. Thank you so much for your answer! – user2171504 Mar 14 '13 at 20:24
  • @user2171504: C, and many other languages (and even some shell programs) use this syntax. So, it's not _absolutely_ crazy. However, it _is_ confusing to beginners—which is why Python 3.0 finally did away with it. (C is also where the `0x1f` syntax for hex came from, and the new `0x123` syntax for octal is obviously based on the same idea.) – abarnert Mar 14 '13 at 20:25
  • @abarnert, I thought Python 3 would be the perfect time to make leading zeros work the way normal humans expect them to work, i.e. ignore them. I'm not pleased that they're invalid now. – Mark Ransom Mar 14 '13 at 20:28
  • @abarnert: I'll keep an eye out in my C code and shell scripts from now on. Thank you! – user2171504 Mar 14 '13 at 20:31
  • 4
    @MarkRansom: You're forgetting that Python has to make sense to programmers coming from C-family languages, sysadmins who spend more time with chmod than python, and of course long-time Python 2.x programmers, not _just_ "normal humans". There was an argument about this; it's not something Guido just decided in isolation. If you can't do what people expect, an error is better than doing the wrong thing. And if the expectations of 60% and the expectations of 30% are directly contradictory, you can't do what people expect. – abarnert Mar 14 '13 at 21:05
  • @abarnert, other languages have trained you to use braces but I don't see Guido paying any attention to that precedent. Although since braces will probably give a syntax error, maybe he's being consistent. – Mark Ransom Mar 14 '13 at 21:08
  • @MarkRansom: There's two parts to the answer. The first, you already gave: braces will give you a syntax error, rather than silently do the wrong thing. The second is that a language designer has to choose his battles. Indentation and colons make the code easier to read. `1/2` returning `0.5` and `(-1)**.5` returning `0+1j` let you avoid thinking about types of numeric. `013` being decimal 13 lets you line up your code in columns without spaces. In every case, you have to weigh that benefit against the cost of surprising or confusing people. – abarnert Mar 14 '13 at 21:14
2

When you start a number with a 0, Python sees it as an octal. So while yes, octal 04 is equal to decimal 04, but octal 08 does not even exist (because octal digits can only be in the range [0,7]).

Found here: Python: Invalid Token

Community
  • 1
  • 1
Valdogg21
  • 1,151
  • 4
  • 14
  • 24