Interpreters vs. Compilers: How Python Translates Your Code
The CPU understands only machine language, represented in zeros and ones. High-level languages like Python are invisible to the CPU's hardware.
The Gap Between Python and the CPU
When you write Python, you use a high-level language designed to be readable by people. The CPU does not directly understand Python, however. It understands machine language, represented in zeros and ones. A translator is therefore needed to bridge the gap between the language used by programmers and the instructions the CPU can execute.
Two Languages, Two Audiences
| Feature | High-level language | Machine language |
|---|---|---|
| Primary audience | Programmers and other humans | CPU hardware |
| Representation | Readable words and syntax, such as Python statements | Zeros and ones |
| Portability | Can run on different machines when a suitable translator is available | Tied to a particular CPU type |
| How it is used | Programmers write programs in it | The CPU executes it |
| Writing difficulty | Designed to make programming practical for humans | Intricate and impractical for most programmers to write directly |
Machine language may appear simple because it uses only two symbols. That appearance is misleading. The source material explains that machine language has intricate syntax and requires details such as which CPU registers to use, which memory locations to access, and which low-level operations to perform. The small number of symbols does not make the language easy to write.
A Small Statement, A Large Translation
Following x = 5 + 3
What must happen before the CPU can perform the computation represented by the Python statement x = 5 + 3?
1. Human-readable source: The programmer writes x = 5 + 3 in Python. The statement is concise and readable.
2. Translation: A translator converts the Python statement into a long sequence of machine-language instructions. The result is not merely a simple binary spelling of the Python line; it is a different way of expressing the same computation for the CPU.
3. Low-level details: The machine-language sequence specifies details such as which CPU registers to use, which memory locations to access, and which low-level operations to perform.
4. Execution: The translated instructions are loaded into memory and executed by the CPU.
A short Python statement can require a long, detailed sequence of hardware-specific machine-language instructions.
What the Translator Does
When you run a Python program, the source describes a translator reading the Python code line by line and converting it into machine language. The resulting machine code is tailored to the specific CPU. It is then loaded into memory, where the CPU executes the binary instructions.
Portability Through Re-translation
Machine language is hardware-specific. Different processor types, including Intel, ARM, and AMD processors named in the source, can use different machine languages. Machine code written for one processor therefore cannot simply be treated as the machine code for another processor. High-level Python code gains portability because it can be translated again on the target computer.
Sending a Program to Another Computer
Why can a Python program run on a friend's computer even when that computer uses a different CPU?
1. Keep the Python source: The same high-level Python program is sent to the friend's computer.
2. Use the translator there: A translator installed on the friend's machine reads the Python program.
3. Produce target-specific instructions: The translator converts the program into machine language suited to the friend's CPU.
4. Execute the result: The friend's CPU executes the machine language produced for its own hardware.
The Python source can remain the same even though the machine language produced on the two computers may be different.
Mistakes About Translation
Assuming that the CPU directly understands Python.
The CPU understands machine language, not high-level Python code.
Fix:
Remember that a translator converts the Python program into machine language before the CPU executes it.Assuming that zeros and ones make machine language easy to write.
Machine language uses only two symbols but still has intricate syntax and must specify low-level CPU details.
Fix:
Judge a language by the complexity of the instructions it expresses, not by the number of symbols it uses.Assuming that every computer uses the same machine language.
Machine language is tied directly to CPU hardware.
Fix:
Distinguish portable Python source from the hardware-specific machine language produced by translation.Assuming that portability means identical machine code everywhere.
Each computer can translate the same Python source into machine language for its own CPU.
Fix:
Portability comes from re-translation on the target machine, not from identical machine-language output.
Check Your Understanding
A Python program is sent from one computer to a friend's computer, which uses a different CPU. Explain why the program can still run without changing its Python source code. Include the roles of the translator, machine language, and CPU.
Hints
- Start with what the CPU understands.
- Explain what happens on the friend's computer.
- State whether the resulting machine language must be identical.
What do you think happens?
If two different CPU types run the same Python program, must they execute identical machine-language instructions?
Reveal answer
Answer: No, each CPU can receive machine language suited to its own hardware.
The same Python program can be translated separately on different computers. The resulting machine language may differ because machine language is hardware-specific.
The Translation Chain
- A CPU understands machine language, represented by zeros and ones, rather than Python.
- A translator converts readable Python code into machine language that the CPU can execute.
- Machine language's binary appearance hides intricate, hardware-specific details.
- Python gains portability because the same source can be translated again for a different CPU.
- Different target computers may produce different machine-language instructions from the same Python program.
Key Takeaways
- CPUs directly understand machine language, not Python.
- Translators bridge the gap between human-readable high-level code and CPU-executable instructions.
- Machine language is more intricate to write than Python despite using only zeros and ones.
- Machine language is hardware-specific, while Python gains portability through re-translation.
- The same Python source can lead to different machine-language instructions on different CPUs.