Interpreters: Translating Code Line by Line
A compiler translates an entire program from high-level source code to machine language in one complete pass, then saves the result to an executable file for later execution.
Two Translation Workflows
When you write a program, the computer cannot directly execute the high-level instructions intended for human programmers. A translation process must connect your source code to machine language. A compiler translates the entire program in one complete pass and saves the result for later execution. An interpreter instead translates and executes source code line by line, allowing a Python programmer to see results as execution proceeds.
The key difference is when execution begins. With a compiler, you first provide the complete source file. The compiler reads the entire file, checks for errors, translates its statements into machine instructions, and produces a new executable file. With an interpreter, source code is processed sequentially: a line is translated and executed before the interpreter moves to the next line.
What a Compiler Produces
- Write the source code in a text editor and save it as a file.
- Run the compiler on the source file.
- The compiler reads the entire file, checks for errors, and translates every statement into machine instructions.
- The compiler creates a new file containing machine language.
- The operating system can later run the executable file.
The result of compilation contains low-level byte sequences designed for CPU execution rather than human reading. Loops, conditionals, and function calls from the original high-level program have already been translated into these machine-language sequences. Opening such a file in a text editor therefore produces symbols and control characters that are not meaningful as ordinary human-readable source code.
| Operating system | How an executable is identified | What the file contains |
|---|---|---|
| Windows | Typically a .exe or .dll suffix | Machine language instructions |
| Linux | File permissions mark it as runnable rather than a special suffix | Machine language instructions |
| macOS | File permissions mark it as runnable rather than a special suffix | Machine language instructions |
Executable identification differs by operating system, while the executable content is machine language.
Python Behind the Scenes
Python is used as an interpreted language: the Python interpreter reads Python source code and translates and executes it line by line. However, the interpreter itself is not written in Python. The Python interpreter is a compiled program written in C. Python developers used a C compiler to create that compiled program, and that program then reads and executes Python code.
This creates two layers of translation. First, a C compiler creates the Python interpreter as a compiled program. Later, when you launch Python, that compiled interpreter reads your Python source code and interprets it. Therefore, saying that Python is interpreted does not mean that every program involved was created without compilation.
Tracing Values Through Lines
A variable is a symbolic name that stores a value in memory, allowing a program to refer to data by name rather than by its literal value. During sequential execution, a line can use the results of earlier lines, but it cannot reference lines that come later.
total = 4 bonus = 3 total = total + bonus print(total)
What do you think happens?
What does this program display? points = 5 extra = 2 points = points + extra print(points)
Reveal answer
Answer: 7
The interpreter processes the assignments in order. The third line retrieves 5 from points and 2 from extra, adds them, and stores 7 under points. The final line displays the current value of points.
Interactive Python Sessions
Python's interpreted execution model supports interactive programming. You can provide a command, have the interpreter translate and execute it, and see the result immediately. Each later command can build on values established by earlier commands. This is the practical effect of processing code sequentially: the interpreter works with the current state created by the lines that have already run.
12The final output is 12 because quantity is assigned 3, price is assigned 4, and cost is then assigned the result of using those earlier values. The print statement retrieves the current value of cost. If a later line attempted to use a name before an earlier line had assigned it, the sequential trace would not have that earlier value available.
Mistakes in Translation Models
Assuming an interpreter translates the entire program before anything runs.
An interpreter translates and executes source code line by line, which makes immediate interactive results possible.
Fix:
Track the program sequentially: each line is processed using the results available from earlier lines.Assuming that Python has no compiled component.
The Python interpreter itself is a compiled program written in C.
Fix:
Separate the two stages: a C compiler created the Python interpreter, and that interpreter later reads Python source code.Treating a variable name as if it were the value itself.
The assignment stores the calculated result under the name total, replacing the value that total referred to before.
Fix:
Update your trace after every assignment and record the current value associated with each name.Expecting executable files to be identified the same way on every operating system.
Windows typically uses .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.
Fix:
Identify the operating system before applying a rule about executable files.
Practice the Trace
Trace this program from top to bottom. Write the value associated with each variable after every assignment, then predict the output. first = 6 second = 1 result = first + second first = result + 2 print(first)
Hints
- Begin with first = 6 and second = 1.
- The third line uses the values already associated with first and second.
- The fourth line changes the value associated with first.
- The final line displays the current value of first, not its original value.
Tracing the practice program
Determine the output of the program and the variable values immediately before the print statement.
First assignment: first refers to 6.
Second assignment: second refers to 1.
Calculate result: The interpreter retrieves 6 and 1, adds them, and assigns 7 to result.
Update first: The interpreter retrieves result, adds 2, and assigns 9 to first. The earlier value 6 is no longer the current value associated with first.
Display the value: print(first) retrieves the current value associated with first.
The program displays 9. Immediately before printing, first refers to 9, second refers to 1, and result refers to 7.
Key Takeaways
- A compiler translates an entire high-level program into machine language and saves the result in an executable file for later execution.
- Windows typically identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.
- The Python interpreter is itself a compiled program written in C.
- An interpreter translates and executes source code line by line, which supports Python's interactive execution model.
- Variables are symbolic names whose current values can be retrieved and updated as the interpreter processes a program sequentially.
Key Takeaways
- Compilers process a complete program and produce machine-language executables before execution.
- Interpreters process source code line by line and execute each line as it is translated.
- Python source code is interpreted by a compiled interpreter written in C.
- Variables provide symbolic names for values, and assignments can update those names during sequential execution.
- To predict Python output, trace each line in order and update the current value of every variable.