Python compilers

Martin v. Loewis martin at v.loewis.de
Wed Apr 3 02:24:08 EST 2002


Rob Hazlewood <rob at moofed.com> writes:

> I'm thinking of learning python, and am a little confused as to the
> overall structure of python programming.
> 
> Would I be right in saying that Python is an interpreted language?

No: there are no "interpreted languages" and "compiled languages";
there are only interpters for languages, and compilers for languages.
The same language may be executed under each model, or in a mixed
approach (like Java).

> I know it can be run as an interpreted language, however I seem to
> remember people talking about python compilers.
> 
> Do Python compilers exist? are they efficient?

Python interpreters (both CPython and Jython) generate byte code
before executing anything - this is a compilation step.

There have been attempts to write compilers to compile Python to C
code; those gave only minimal speedup.

There is also an ongoing attempt to perform just-in-time compilation
(Psyco); this gives measurable speed-up, for selected applications.

> If there is a python compiler, surely it would be able to generate
> code that is as efficient as a C compiler, resulting in what would be
> the best available OO language? (having simplicity, speed)

The Python interpreter is written in C, so it already runs at C speed.

A Python compiler most likely would *not* be able to produce code that
is as fast as code that a C compiler could produce for an "equivalent"
C program - because both programs can't possibly be "equivalent".

Just consider the program

int add(int x, int y){return x+y;}

In C, the compiler can compile this to a machine program that uses the
processor's integer addition, and expects arguments on the stack. The
"equivalent" Python program is

def add(x,y):return x+y

Right? Wrong. The Python function can also be used to do

print add("Hello,","World")

So a compiler compiling the Python version needs to take the
possibility into account that you are not passing integers, but
strings - you can't know, at compile time, what "+" is.

HTH,
Martin



More information about the Python-list mailing list