Compiler and Interpreter ======================== .. toctree:: :maxdepth: 1 Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` .. _section_heading-Python_Implementations: Python Implementations ---------------------- Inside the Python :term:`interpreter`, the Python code you write is compiled down to :term:`bytecode` and executed by the :term:`Virtual machine` with the results being passed back to the interpreter for display. This is shown diagrammatically in :numref:`figure-py_imp_compiler_interpreter_vm` .. _figure-py_imp_compiler_interpreter_vm: .. figure:: compiler_interpreter_vm.png :scale: 100 % :align: center Compiler-Interpreter-Virtual Machine 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* :term:`bytecode` and executed on the *Python* :term:`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 :term:`bytecode` compiler, :term:`virtual machine` and :term:`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 :term:`bytecode` compiler and :term:`virtual machine`. .. admonition:: Try it! :class: TryIt The CPython :term:`bytecode` shown in :numref:`figure-py_imp_compiler_interpreter_vm` is real. You can obtain it yourself by executing the following in the python/ipython console, or in jupyter-notebook. >>> import dis >>> >>> def print_hello_world(): ... print("Hello World") ... >>> dis.dis(print_hello_world) .. _CPython: https://en.wikipedia.org/wiki/CPython .. _Jython: https://en.wikipedia.org/wiki/Jython .. _IronPython: https://en.wikipedia.org/wiki/IronPython .. _PyPy: https://en.wikipedia.org/wiki/PyPy .. _suite: https://docs.python.org/3.5/reference/compound_stmts.html#grammar-token-suite .. _dedent: https://docs.python.org/3.5/library/token.html?highlight=dedent#token.DEDENT .. _standard_encodings: https://docs.python.org/3.5/library/codecs.html#standard-encodings