Understanding Python's interpreter

Rafael Almeida rafaelc at dcc.ufmg.br
Sat Apr 7 02:20:24 EDT 2007


Hello,

I'm studying compilers now on my university and I can't quite
understand one thing about the python interpreter. Why is its input a
binary file (pyc)? The LOAD_CONST opcode is 100 (dec) and STORE_FAST's
is 125 (dec). The translation of the following code:

foo.py:
	x = 10

Could be this:

foo.pyc:
	100 10
	125 0

That way you wouldn't need code such as 
        static void
        w_long(long x, WFILE *p)
        {
                w_byte((char)( x      & 0xff), p);
                w_byte((char)((x>> 8) & 0xff), p);
                w_byte((char)((x>>16) & 0xff), p);
                w_byte((char)((x>>24) & 0xff), p);
        }
since you could read it with strtol and write back using a simple
printf. So you wouldn't need a buch of casts by simple using ascii
input and output.

What's the reason for having it in a binary form instead of writting
it in ascii? (yeah, I know ascii would be binary also, but I think you
get my point.)

So I was writting this interpreter for some assembly language defined
in class and I did something like python does, so I had a binary file
to interpret. After a while I thought it was far harder to program.
And when I tried to code an assembler my problems got greater, as I
wanted to code it in python (the interpreter was in C++) and I had a
hard time trying to figure out how I would print something that's not a
ascii or unicode string. As for the benefits, I couldn't figure out any.

I hope I'm not too offtopic here, but I thought this was probably the
best news to ask this. If someone thinks there's another news that's
more appropriate, please tell me.

[]'s
Rafael



More information about the Python-list mailing list