eval('07') works, eval('08') fails, why?

  • Thread starter Alex van der Spek
  • Start date
A

Alex van der Spek

I am baffled by this:

IDLE 1.2.2 ==== No Subprocess ====
08
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
input()
File "<string>", line 1
08
^
SyntaxError: invalid token

of course, I can work around this using raw_input() but I want to
understand why this happens. It boils down to:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval('08')
File "<string>", line 1
08
^
SyntaxError: invalid token

I can't think of anything that could cause this. Similarly, eval('09')
fails, but for string 0x with x<8 it works. I am teaching myself Python
in order to climb the ladder from Algol(1980s)-->Pascal(1990s)--
VisualBasic(2000)-->Python. I am a physicist, have programmed computers
all my life but I won't understand the real tech jargon of present day
computer science. Please keep it simple

Thanks in advance,
Alex van der Spek
 
M

Mark Dickinson

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    eval('08')
  File "<string>", line 1
    08
     ^
SyntaxError: invalid token

An integer literal with a leading zero is
interpreted as an octal (base 8) number,
so only digits in the range 0-7 (inclusive)
are permitted in such a literal.

Mark
 
N

Ned Deily

Well, that's the correct explanation.
Whether that feature makes sense or not is debatable.

The debate is over! In Py 3.0, octal literals changed from 07 to 0o7;
the old format gets an 'invalid token' parsing error.
 
T

Terry Reedy

Alex said:
I can't think of anything that could cause this. Similarly, eval('09')
fails, but for string 0x with x<8 it works. I am teaching myself Python
in order to climb the ladder from Algol(1980s)-->Pascal(1990s)--
all my life but I won't understand the real tech jargon of present day
computer science. Please keep it simple

I taught myself Python as a statistician with a Fortran/C backgound by
interleaving interactive experiments (such as you did) with reading of
the manuals. The Language Manual chapter on Lexical Analysis has an
Integer Literals subsection that answers this question. I strongly
recommend you peruse the Language Manual and the initial Library Manual
chapters on built-ins.

tjr
 
B

Bruno Desthuilliers

Grant Edwards a écrit :
Well, that's the correct explanation.

Whether that feature makes sense or not is debatable. Even I'm
not old-school enough that I ever use octal literals -- and I
used Unix on a PDP-11 for years (actually had my own PDP-11 for
while, but it never worked). Now that I think of it, my
Heathkit Z80 stuff used octal notation too.

What about your DeathStation 9000 ?-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top