Understanding Python Implementations

Shashwat Singh
Jul 19 · 3 min read
Image for post
Image for post
Photo by Hitesh Choudhary on Unsplash

An “implementation” of Python should be taken to mean a program or environment which provides support for the execution of programs written in the Python language.

The above definition is stated in the python reference documents. To explain this concept in simple terms, we’ll need to understand a few general terms related to code execution on any machine.

Compiler is a function that converts one programming language to another, typically from a high level language(C, C++) to a low level language(Machine code, Assembly language) to create an executable file.

Interpreter is a function that directly executes a programming language without it being compiled and converted to machine code.

Python is considered as an interpreted language, but this is partially true, as it involves a compilation step that converts the python code into bytecode which is stored with a .pyc or .pyo format that gets deleted once the program is executed. Bytecode is also binary representation executed by virtual machine (not by CPU directly). The virtual machine (which is written different for different machines) converts binary instruction into a specific machine instruction.

Image for post
Image for post
Python code execution

Above diagram explains Python’s code execution flow. The source code is first compiled and converted to a bytecode. This bytecode is then executed on a virtual machine. Python implementations are defined based on the language these virtual machines are build on or the way it is interpreted/compiled.

The default python(that we download from https://www.python.org/) is also known as CPython is the reference implementation of python. This is because the bytecode produced by the compiler of this implementation is run by a virtual machine that is written using C code. When the python.exe file is executed, it compiles the C code of this virtual machine and generates a machine code. This virtual machine now acts as an emulator of the CPU and runs bytecode the same way a CPU would run machine instructions. CPython is distributed with a large standard library written in a mixture of C and Python. CPython provides the highest level of compatibility with Python packages and C extension modules. All versions of the Python language are implemented in C because CPython is the reference implementation.

  • Jython: This is a Java implementation of python and can run on java platform. It compiles into java bytecode that can be run in a java virtual machine. This uses java classes instead of python modules. Below is an example of Jython code.
import org.python.util.PythonInterpreter;

public class JythonHelloWorld {
public static void main(String[] args) {
try(PythonInterpreter pyInterp = new PythonInterpreter()) {
pyInterp.exec("print('Hello Python World!')");
}
}
}
  • IronPython: Similar to Jython, this is written in C# and uses .Net virtual machine(Common Language Runtime). IronPython does not has a Global Interpreter Lock(GIL) like CPython and hence performs better for multi-threaded applications. Below is an example to IronPython code.
import sys
sys.path.append(r'C:\Python24\Lib')

import clr
clr.AddReference("System.Windows.Forms")

from System.Windows.Forms import Application, Form

class HelloWorldForm(Form):

def __init__(self):
self.Text = 'Hello World'
self.Name = 'Hello World'

form = HelloWorldForm()
Application.Run(form)
  • PyPy: This is an implementation of python that is written itself in python(RPython). The reference python(CPython) converts the code into bytecode and then uses interpreter to run this which is slow as compared to machine code. PyPy uses JIT(Just In Time compilation) which involves compilation of the code during its execution time and is much faster. Below is an example in PyPy code.
def entry_point(argv):
print "Hello, World!"
return 0

# The target function is the main function of a RPython program. It # takes command line arguments as inputs and return an entry point # function.
def target(*args):
return entry_point

"If you want your code to run faster, you should probably just use PyPy."
-- Guido van Rossum (creator of Python)

  • Brython: a way to run Python in the browser through translation to JavaScript.
  • PyMite: Python for embedded devices.
  • pyjs: (formally Pyjamas) a Python to JavaScript compiler plus Web/GUI framework.

References

The Startup

Medium's largest active publication, followed by +716K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store