How does Python work? and various Python Implementations

How does Python work? and various Python Implementations

Learn about CPython and how does python executes code and various implementations of python and python compilers

This article is part of my CPython exploration. In this article, we will learn about CPython, Compilers and how does python executes code and various implementations of python.

Compiler

In computing, a compiler is a program that translates code written in one programming language into another language. To achieve this, the compiler must understand grammatical structures of both languages.

Some compilers will compile into a low-level machine code that can be executed directly on a system. Other compilers will compile into an intermediate language(bytecode) to be executed by a virtual machine so that compiled code is portable across multiple platforms.

Self-hosted compilers

Self-hosted compilers are the compilers written in the language they compile, such as Go compiler. This is done by a process known as bootstrapping.

For example, C, C++, Go, and Pascal will compile source code into an executable binary. This executable binary is built for the platform on which it was compiled. The first go compiler was written in C, then once Go could be compiled, the compiler was rewritten in Go.

Source-to-source compilers

Source-to-source compilers are compilers written in another language that already has a compiler.

If you're writing a new programming language from scratch, then you need an executable application to compile your compiler. you need a compiler to execute anything, so when new languages are developed, they're often written first in an older more established language.

CPython

The C in CPython is a reference to the C programming language, indicating that this python distribution is written in C language. The compiler in CPython is written in pure C language and it is a source-to-source compiler. However many of the standard library modules are written in pure Python or a combination of C and Python.

Source code to Execution flow in CPython

cpython.png

  • The source code and tokenised and parsed to produce a parse tree.
  • Parse tree is then transformed into Abstract Syntax Tree.
  • Abstract Syntax Tree is then transformed into Control Flow Graph(CFG).
  • Emit bytecode as .pyc files based on Control Flow Graph
  • The Interpreter or Python VM takes the bytecode and produces output.

Pypy

There is a python compiler written in python called PyPy. It is based on Just-in-Time compiler. Just-in-Time compilation is a way of executing computer code that involves compilation during execution of a program rather than before execution. PyPy is 4.2 times faster than CPython.

Jython

An example for a cross compiler for python is Jython. Jython in written in Java and compiles from python source code into java bytecode. The same way Cpython makes it easy to import C libs and use them in python, Jython makes it easy yo import java modules and classes.

Cython

a widely used optimising Python-to-C compiler, CPython extension module generator, and wrapper language for binding external libraries. Interacts with CPython runtime and supports embedding CPython in stand-alone binaries.

Numba

NumPy-aware optimizing runtime compiler for Python mostly useful for scientific computation environments.

Have any questions or suggestions? Please comment below. Learnt something new? Share this article with others who may find this useful. Stay tuned for upcoming articles. Subscribe to the newsletter and Connect with me on twitter to get my future articles.

Did you find this article valuable?

Support Suresh Kumar by becoming a sponsor. Any amount is appreciated!