Understanding Python's interpreter

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 6 22:35:49 EDT 2007


Rafael Almeida wrote:

> 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.

A "simple" printf or strtol involves hundreds of instructions. And 
people want Python to execute fast...

> 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.)

Speed? Eficiency? File size? Ease of use?
A .pyc *could* be written in ASCII, but what do you gain? Replacing a 
few trivial functions in the Python core with a printf/scanf equivalent? 
  At the same time you lose a lot of speed, so I don't see the 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.

Why harder? Once you read the file, they're just numbers. Anyway, being 
harder to program the *interpreter* is not a problem, if you gain 
something like speed or eficiency for the interpreted language.

> 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.

Sorry, I could not understand what you said here.

-- 
Gabriel Genellina





More information about the Python-list mailing list