Virtual Machines and Bytecode
Python is a language specification; a Python implementation is the software that executes code written in that language.
Rules and Runtimes
When people say that they are running Python, they may be referring to two different things. Python is a language specification: a set of rules that describes how Python code is written and what the language means. A Python implementation is software that reads and executes code written according to those rules. The specification is the shared definition; the implementation is the working tool that makes programs run.
A Python implementation is software that executes code written in the Python language specification.
From Source to Execution
The central execution story has three useful ideas: Python source code, bytecode, and a virtual machine or runtime that carries out the work. The implementation is the component that connects these ideas. It takes responsibility for interpreting or otherwise executing the Python program rather than leaving the language specification as an abstract set of rules.
The important boundary is not between a file and a command prompt. It is between the language rules and the implementation that gives those rules operational behavior.
CPython as the Standard
CPython is the reference implementation and the most widely used Python interpreter. When Python is installed from python.org and you run python in a terminal, you are using CPython. It is written in C, so the C in CPython identifies the language used to write its interpreter. The material also describes CPython as the Classical Python interpreter because it is the original reference implementation.
CPython has an influential role in the Python ecosystem. New language features and language changes are first implemented in CPython, and other implementations follow its standard. Its C implementation also contributes to its speed and efficiency, while its support across Windows, macOS, Linux, and other operating systems contributes to its broad use.
Choosing an Implementation
Multiple implementations exist because one implementation cannot optimize equally for every platform and use case. CPython is the usual starting point because it is the reference standard, widely used, and supported by the largest ecosystem of third-party libraries. Other implementations become valuable when a project has a specific integration or performance requirement.
| Implementation | Primary strength | When it is a sensible choice |
|---|---|---|
| CPython | Reference implementation, broad use, portability, and ecosystem support | As the default choice for most projects |
| Jython | Integration with Java | When Python code must use Java libraries and run within the Java Virtual Machine |
| IronPython | Integration with .NET | When Python code must use .NET libraries and classes |
| PyPy | Performance optimization through JIT compilation | When long-running programs or tight loops may benefit from faster execution |
Different implementations preserve Python as the language while emphasizing different environments or goals.
Selecting an Implementation for a Project
A team wants to write Python code in an organization whose existing software and libraries are centered on Java. Which implementation best matches that requirement?
Identify the constraint: The project needs deep access to the Java platform and its libraries, not merely a Python program that runs near a Java application.
Match the implementation: Jython is the Python implementation designed for Java integration. It runs within the Java Virtual Machine and allows Python code to instantiate Java classes, call their methods, and pass data back and forth.
Reject the default-only assumption: CPython is the usual default, but the platform requirement gives Jython a specific advantage for this project.
Jython is the appropriate candidate when deep integration with Java is the main requirement.
Java and .NET Bridges
Jython and IronPython demonstrate that an implementation can be designed around a host platform. Jython runs Python within the Java Virtual Machine. Python code can instantiate Java classes, call their methods, and pass data between Python and Java. This is deep integration rather than an indirect connection to a separate program.
IronPython provides the corresponding path into the .NET ecosystem. It runs within the .NET runtime and can interact with .NET libraries and classes, including libraries written in C#, the primary language of .NET identified in the source material. These implementations let organizations use Python's simplicity and expressiveness while remaining connected to established Java or .NET codebases.
PyPy and Self-Implementation
PyPy is a research project that implements Python in Python itself. More precisely, PyPy is written in RPython, a restricted subset of Python. RPython is then translated into C and compiled. This does not mean that PyPy runs without an interpreter; it means that the logic for parsing and executing Python code is expressed in a Python-based language before the implementation is translated and compiled.
This design changes how the interpreter is developed. CPython developers write interpreter changes in C, then recompile and test. PyPy developers can express changes in Python, the language family they are implementing. That can make experimentation faster and the codebase more accessible to Python developers. PyPy also includes a just-in-time compiler, or JIT compiler, which can make Python programs significantly faster than CPython for many workloads, especially long-running programs with tight loops.
Mistakes to Avoid
Treating Python and CPython as exactly the same thing.
Python is the language specification, while CPython is one implementation of that language, written in C.
Fix:
Say that CPython is the C-written reference implementation of Python.Assuming that CPython is the only Python implementation.
Jython and IronPython target those host ecosystems, and PyPy has a different performance and development focus.
Fix:
Start with CPython by default, then check whether the project has a specific reason to use another implementation.Assuming that PyPy's Python-based design means it has no compiled component.
PyPy is written in RPython, which is translated into C and compiled.
Fix:
Describe PyPy as a Python-based implementation whose RPython source is translated into C and compiled.Expecting PyPy to be faster in every situation.
Its JIT compiler can provide significant gains in many scenarios, especially long-running programs with tight loops, but the source recommends benchmarking.
Fix:
Benchmark performance-critical code before choosing PyPy for speed.
Portable Python Choices
A practical strategy is to begin with CPython unless the project has a clear reason to choose another implementation. Choose Jython when Java integration is central, IronPython when .NET integration is central, and PyPy when a performance-critical workload may benefit from its JIT compiler. The most portable approach is to write standard Python code that does not depend on implementation-specific features. That keeps the program's options open across implementations.
- Identify the language requirement: the project needs Python behavior described by the language specification.
- Check the host ecosystem: use Jython for deep Java integration or IronPython for deep .NET integration.
- Check the performance requirement: benchmark PyPy when long-running programs or tight loops are important.
- Use CPython as the default when no specialized requirement changes the decision.
- Avoid implementation-specific features when portability across implementations matters.
Check Your Model
A team needs Python code to call classes from an existing .NET codebase. Another team is researching interpreter design and wants to experiment with an implementation expressed in a Python-based language. Which implementations should each team investigate, and why?
Hints
- Match the first requirement to the host platform.
- For the second requirement, focus on the implementation whose interpreter logic is expressed in RPython.
- Explain the choice in terms of the implementation's design goal rather than simply naming it.
What do you think happens?
A developer installs Python from python.org and runs python in a terminal. Which implementation are they using?
Reveal answer
Answer: CPython
The source material identifies CPython as the implementation used when Python is installed from python.org and run with python in a terminal.
Key Takeaways
- Python is a language specification; CPython, Jython, IronPython, and PyPy are implementations that execute Python code.
- CPython is the C-written reference implementation and the usual default when Python is installed from python.org.
- Jython runs within the Java Virtual Machine and provides access to Java classes and libraries.
- IronPython runs within the .NET runtime and provides access to .NET libraries and classes.
- PyPy uses RPython, a restricted subset of Python, and includes a JIT compiler; its performance advantages should be measured on the target workload.
Key Takeaways
- A language specification defines rules; an implementation is software that executes code following those rules.
- CPython is the original, reference, C-written implementation and the default choice for most Python work.
- Different implementations serve different priorities, including Java integration, .NET integration, and JIT-based performance optimization.
- PyPy shows that interpreter logic can be developed in a Python-based language and then translated into C.
- Writing standard Python and benchmarking specialized workloads helps preserve portability and guide implementation choices.