[Tutor] strange eval

Kent Johnson kent_johnson at skillsoft.com
Mon Oct 4 15:24:24 CEST 2004


Number literals starting with 0 are interpreted as _octal_! See 
http://docs.python.org/ref/integers.html
 >>> eval('010')
8
 >>> eval('0100')
64

8 and 9 are not valid octal digits, hence the error.

Kent

At 08:54 AM 10/4/2004 -0400, Doug Penny wrote:
>I have a couple of students working on some programming projects and
>they are experiencing some strange issues with eval.  We are working
>on Windows 2000 boxes using Python 2.3.4.  For the most part eval
>seems to work fine.  For instance, these commands work well:
>
> >>>eval("07")
>7
> >>>eval("10")
>10
>
>However, 08 and 09 do not work.  We get the following error:
>
> >>>eval("08")
>Traceback (most recent call last):
>  File "<pyshell#0>", line 1, in -toplevel-
>    eval("08")
>  File "<string>", line 1
>    08
>     ^
>SyntaxError: invalid token
>
> >>>eval("09")
>Traceback (most recent call last):
>  File "<pyshell#1>", line 1, in -toplevel-
>    eval("09")
>  File "<string>", line 1
>    09
>     ^
>SyntaxError: invalid token
>
>It seems like this are the only two number that have trouble.  Has
>anyone else experienced this problem?  Thanks.
>
>Doug Penny
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list