Is python a interpreted or compiled language?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 20 21:48:34 EDT 2012


On Wed, 20 Jun 2012 18:27:53 -0600, Ian Kelly wrote:

> On Wed, Jun 20, 2012 at 5:30 PM, gmspro <gmspro at yahoo.com> wrote:
>>
>> Hi,
>>
>> Is python a interpreted or compiled language?
> 
> Like other languages that use a VM bytecode, it's a little bit of both. 
> The actual Python code is compiled into Python bytecode.  The bytecode
> is interpreted.

I should point out that the difference between interpreted and compiled 
is fairly arbitrary and less useful these days.

The model for programming languages used to be:

Interpreters go over the source code, line by line, executing each line 
as it is read. If you loop over a line twice, it gets read twice, 
interpreted twice, and executed twice.

Compilers go over the source code once, line by line, interpreting the 
code once, building machine code which can be directly executed by the 
hardware.

Even back in the 1970s this simple picture was not quite right: it hides 
a lot of detail. Many "compiled" languages would compile to assembly 
language for a virtual machine, rather than direct to hardware specific 
machine code. For example, some Pascal compilers would compile code for a 
virtual p-machine. Languages like Forth can switch from "compile" mode to 
"interpret" mode from one command to the next. And of course, machine 
code itself is interpreted by the hardware, and there are many ways of 
setting up the machine code (e.g. direct vs indirect threaded code) which 
may be faster or slower at the expense of more or less memory. So the 
distinction between compiled and interpreted has always been a bit fuzzy.

These days it is a lot fuzzy. Any "compiled" language that includes an 
eval or exec function must include an interpreter; nearly all 
interpreters compile code to byte code, and the few that don't usually 
build a parse tree instead. 

And then there are Just In Time compilers, which compile to machine code 
instructions at runtime, giving the flexibility of interpreters with the 
speed of compilers. An interpreter with a clever enough JIT can be faster 
than a static compiler, which leads to cases where Python can be faster 
than C:

http://morepypy.blogspot.com.au/2011/02/pypy-faster-than-c-on-carefully-crafted.html

http://morepypy.blogspot.com.au/2011/08/pypy-is-faster-than-c-again-string.html


-- 
Steven



More information about the Python-list mailing list