82

This is just a wondering I had while reading about interpreted and compiled languages.

Ruby is no doubt an interpreted language since the source code is processed by an interpreter at the point of execution.
On the contrary C is a compiled language, as one have to compile the source code first according to the machine and then execute. This results is much faster execution.

Now coming to Python:

  • A python code (somefile.py) when imported creates a file (somefile.pyc) in the same directory. Let us say the import is done in a python shell or django module. After the import I change the code a bit and execute the imported functions again to find that it is still running the old code. This suggests that *.pyc files are compiled python files similar to executable created after compilation of a C file, though I can't execute *.pyc file directly.
  • When the python file (somefile.py) is executed directly ( ./somefile.py or python somefile.py ) no .pyc file is created and the code is executed as is indicating interpreted behavior.

These suggest that a python code is compiled every time it is imported in a new process to create a .pyc while it is interpreted when directly executed.

So which type of language should I consider it as? Interpreted or Compiled? And how does its efficiency compare to interpreted and compiled languages?

According to wiki's Interpreted Languages page, it is listed as a language compiled to Virtual Machine Code, what is meant by that?

Robert Harvey
  • 200,592
crodjer
  • 1,039

5 Answers5

88

It's worth noting that languages are not interpreted or compiled, but rather language implementations either interpret or compile code. You noted that Ruby is an "interpreted language", but you can compile Ruby à la MacRuby, so it's not always an interpreted language.

Pretty much every Python implementation consists of an interpreter (rather than a compiler). The .pyc files you see are byte code for the Python virtual machine (similar to Java's .class files). They are not the same as the machine code generated by a C compiler for a native machine architecture. Some Python implementations, however, do consist of a just-in-time compiler that will compile Python byte code into native machine code.

(I say "pretty much every" because I don't know of any native machine compilers for Python, but I don't want to claim that none exist anywhere.)

mipadi
  • 7,533
36

Python will fall under byte code interpreted. .py source code is first compiled to byte code as .pyc. This byte code can be interpreted (official CPython), or JIT compiled (PyPy). Python source code (.py) can be compiled to different byte code also like IronPython (.Net) or Jython (JVM). There are multiple implementations of Python language. The official one is a byte code interpreted one. There are byte code JIT compiled implementations too.

For speed comparisons of various implementations of languages you can try here.

Maroun
  • 103
aufather
  • 4,427
13

Compiled vs. interpreted can be helpful in some contexts, but when applied in a technical sense, it is a false dichotomy.

A compiler (in the broadest sense) is a translator. It translates program A to program B and for future execution it using a machine M.

An interpreter (in the broadest sense) is an executor. It is a machine M that executes program A. Though we typically exclude from this definitions physical machines (or non-physical machines that act just like physical ones). But from theoretic perspective, that distinction is somewhat arbitrary.


For example, take re.compile. It "compiles" a regex to an intermediate form, and that intermediate form is interpreted/evaluated/executed.


In the end, it depends on what level abstraction you are talking about, and what you care about. People say "compiled" or "interpreted" as broad descriptions of the most interesting parts of the process, but really most every program is compiled (translated) and interpreted (executed) in one way or another.

CPython (the most popular implementation of the Python language) is mostly interesting for executing code. So CPython would typically be described as interpreted. Though this is a loose label.

Paul Draper
  • 6,080
7

Virtual Machine Code is a more compact version of the original source code (byte code). It still needs to be interpreted by a virtual machine, since it is no machine code. It's easier and faster to parse than the original code written by a human being, though.

Some virtual machines generate machine code while interpreting the virtual machine code for the first time (just in time compilation - JIT). The following invocations will use this machine code directly, which leads to faster execution.

As far as i know Ruby >= 1.9 uses a virtual machine like Python too.

5

The Python runtime runs custom object code(byte code) on a virtual machine.

The compilation process converts source code to object code.

To speed things up, the object code (or byte code, if you prefer) is stored on disk, so it can be reused the next time the program is run.

ykombinator
  • 4,297