Compiler and Interpreter¶
Indices and tables¶
Python Implementations¶
Inside the Python interpreter, the Python code you write is compiled down to bytecode and executed by the Virtual machine with the results being passed back to the interpreter for display. This is shown diagrammatically in Fig. 17
All of this is completely transparent to you and if you don’t want to, you don’t have to worry about any of it. However, a little additional Python under-the-hood knowledge may help you in the future.
If you consider the Python specification as an interface, it follows that the Python interpreter (containing the compiler and virtual machine) are an implementation of that interface. As the user, you have a choice of implementations.
- CPython: The reference Python implementation. Your Python code is compiled down to Python bytecode and executed on the Python virtual machine. This is currently best option for writing C-extensions as CPython is written in…you guessed it, C.
- Jython: A Python implementation, written in Java, that compiles your Python code down to Java bytecode and executes it on the Java virtual machine. Allows you to use Java classes in your program.
- IronPython: A Python implementation, written in C#, that compiles your Python code down to .NET bytecode and executes it on the .NET Common Language Runtime virtual machine. Allows you to use .NET classes in your program.
- PyPy: A Python implementation, itself written using a subset of the Python language, that compiles your Python code down to machine code (doesn’t use a virtual machine at all). Provides significant speedup, up to 7.5x faster (current benchmarks) than CPython, for certain types of programs.
These are just a few examples of alternate implementations. There are others!
Right now, you may be asking, “Which Python implementation am I using?”. Unless you have specifically selected one, the one you are using is CPython.
Note
You’ve probably noticed the terms bytecode compiler, virtual machine and interpreter used interchangeably and that’s because for most things there is no benefit to being explicit. The difference only matters when you talk about Python implementations and the option to have a different bytecode compiler and virtual machine.