Why is it impossible to create a compiler than can compile Python to machinecode like C?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Mar 1 03:55:53 CET 2013


On Thu, 28 Feb 2013 15:50:00 -0500, Matty Sarro wrote:

> Python is an interpreted language, not a compiled language.

Actually, *languages* are neither interpreted nor compiled. A language is 
an abstract description of behaviour and syntax. Whether something is 
interpreted or compiled or a mixture of both is a matter of the 
implementation. There are C interpreters and Python compilers.



[...]
> Now, there are places where this line is blurred. For instance perl is
> an interpreted language, but capable of running EXTREMELY fast. Python
> is a little slower, but significantly easier to read and write than
> perl. You also have some weird ones like JAVA which actually have a
> virtual machine, and "half compile" source code into java "bytecode."
> This is then executed by the virtual machine.

Welcome to the 20th century -- nearly all so-called "interpreted" 
languages do that, including Python. Why do you think Python has a 
function called "compile", and what do you think the "c" in .pyc files 
stands for?

The old model that you might have learned in school:

* interpreters read a line of source code, execute it, then read the next 
line, execute it, then read the next one, and so forth...

* compilers convert the entire source code to machine code, then execute 
the machine code.


hasn't been generally true since, well, probably forever, but certainly 
not since the 1980s.

These days, the best definition of "interpreted language" that I have 
read comes from Roberto Ierusalimschy, one of the creators of Lua:

"...the distinguishing feature of interpreted languages is not that they 
are not compiled, but that the compiler is part of the language runtime 
and that, therefore, it is possible (and easy) to execute code generated 
on the fly."

(Programming in Lua, 2nd edition, page 63.)

In that sense, being an interpreter is a feature, and pure compilers are 
deficient.


Oh, by the way, while it is true that the original version of Java used a 
pure virtual machine model, these days many Java compilers are capable of 
producing machine code.

Just to drive home the lesson that *languages* aren't compiled or 
interpreted, but *implementations* are, consider these Python 
implementations with radically different execution styles:

1) CPython, the one you are used to, compiles code to byte-code for a 
custom-made virtual machine;

2) Jython generates code to run on a Java virtual machine;

3) IronPython does the same for the .Net CLR;

4) PyPy has a JIT compiler that generates machine code at runtime;

5) Pynie compiles to byte-code for the Parrot virtual machine;

6) Nuitka includes a static compiler that compiles to machine code;

7) Berp generates Haskell code, which is then compiled and executed by a 
Haskell compiler, which may or may not generate machine code;

8) Pyjamas compiles Python to Javascript;

and others.


And even machine code is not actually machine code. Some CPUs have an 
even lower level of micro-instructions, and an interpreter to translate 
the so-called "machine code" into micro-instructions before executing 
them.



-- 
Steven



More information about the Python-list mailing list