Skip to main content

Programming concepts

What is an Interpreter?

Definition

An interpreter is a program that reads source code and executes it directly, translating as it runs instead of producing a separate compiled artifact first.

Interpreter in more detail

There is no build step to wait for: the interpreter reads the program and carries out its instructions, so editing a file and re-running it is the whole loop. That immediacy is why interpreted languages dominate scripting, data work and teaching, and why they come with a REPL where you can evaluate one expression at a time against live state.

The cost is paid at run time. Work a compiler does once — parsing, resolving names, checking types — an interpreter does repeatedly while executing, which is why straightforward interpretation is markedly slower than compiled code. It also means errors hide until execution reaches them: a typo inside a rarely-taken branch of a Python script can sit undiscovered until it fails in production, where the equivalent in a compiled language would never have built.

Almost nothing in wide use is a pure interpreter today. CPython compiles each module to bytecode, caches it in __pycache__ and interprets that; Ruby and PHP do something similar; V8 and SpiderMonkey interpret JavaScript at first and hand hot functions to a just-in-time compiler that emits real machine code using profiles gathered while running. So the practical distinction from a compiler is not whether translation happens, but when — and whether an artifact you can ship separately comes out of it.

Interpreter example

$ python app.py        # no build artifact; runs immediately
$ node server.js
$ ruby script.rb

REPL — evaluate against live state
  >>> total = 0
  >>> for n in [1, 2, 3]: total += n
  >>> total
  6

Errors surface when execution arrives

  def report(rows):
      if not rows:
          print(rows.lenght)   # typo, no error until this branch
      return len(rows)         # runs fine for months

Hybrid, which is the normal case
  .py  --compile-->  .pyc bytecode  --interpret-->  running
  JS   --interpret-->  profile  --JIT compile-->  machine code

Interpreter — common questions

Is Python interpreted or compiled?
Both, in stages. CPython compiles source to bytecode and caches it in __pycache__, then interprets that bytecode. It is called interpreted because there is no separate machine-code artifact you ship and run on its own.
Are interpreted languages always slower?
Usually slower for compute-heavy code, though just-in-time compilation narrows the gap dramatically, and many programs are bound by I/O or database time where the difference is irrelevant. Numerical libraries also push the hot work into compiled code underneath.
Can the same language be both compiled and interpreted?
Yes. Compiled and interpreted describe implementations, not languages: there are C interpreters and Python compilers. Any language can in principle be implemented either way.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary