Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason why lVals = [1, 08, 2011] throws an exception?

Tags:

python

I have discovered one thing that makes me crazy. If I specify the following list:

lVals = [1, 01, 2011]

then no errors will be displayed, and the same will happen if I use 02,03,04,05,06,07, but in case I use 08 or 09 as the second item in the list, I get the following exception:

>>> a = [26, 08, 2011]
  File "<stdin>", line 1
    a = [26, 08, 2011]
              ^
SyntaxError: invalid token

Also the same behavior appears when I put these numbers (08,09) at the any location within list (eg. [08,10,2011]), even if I try to assign 08 to a single int variable I get the same exception.

Is there any reason why this happens?

like image 642
Artsiom Rudzenka Avatar asked Aug 15 '11 12:08

Artsiom Rudzenka


2 Answers

08 is attempting to parse 8 as an octal digit. It isn't one.

like image 102
Wooble Avatar answered Sep 18 '22 19:09

Wooble


I don't really know Python, but I'd guess it takes the starting 0 as the beginning of an octal literal.

like image 32
lccarrasco Avatar answered Sep 22 '22 19:09

lccarrasco