Cython - an overview

[Cython] is a programming language that makes writing C extensionsfor the Python language as easy as Python itself. It aims to becomea superset of the [Python] language which gives it high-level,object-oriented, functional, and dynamic programming. Its main featureon top of these is support for optional static type declarations aspart of the language. The source code gets translated into optimizedC/C++ code and compiled as Python extension modules. This allows forboth very fast program execution and tight integration with external Clibraries, while keeping up the high programmer productivity forwhich the Python language is well known.

The primary Python execution environment is commonly referred to asCPython, as it is written in C. Other major implementations use Java(Jython [Jython]), C# (IronPython [IronPython]) and Python itself(PyPy [PyPy]). Written in C, CPython has been conducive to wrappingmany external libraries that interface through the C language. Ithas, however, remained non trivial to write the necessary glue code inC, especially for programmers who are more fluent in a high-levellanguage like Python than in a close-to-the-metal language like C.

Originally based on the well-known Pyrex [Pyrex], the Cython projecthas approached this problem by means of a source code compiler thattranslates Python code to equivalent C code. This code is executedwithin the CPython runtime environment, but at the speed of compiled Cand with the ability to call directly into C libraries.At the same time, it keeps the original interface of the Pythonsource code, which makes it directly usable from Python code. Thesetwo-fold characteristics enable Cython’s two major use cases:extending the CPython interpreter with fast binary modules, andinterfacing Python code with external C libraries.

While Cython can compile (most) regular Python code, the generated Ccode usually gains major (and sometime impressive) speed improvementsfrom optional static type declarations for both Python and C types.These allow Cython to assign C semantics to parts of the code, and totranslate them into very efficient C code. Type declarations cantherefore be used for two purposes: for moving code sections fromdynamic Python semantics into static-and-fast C semantics, but alsofor directly manipulating types defined in external libraries. Cythonthus merges the two worlds into a very broadly applicable programminglanguage.

[Cython]G. Ewing, R. W. Bradshaw, S. Behnel, D. S. Seljebotn et al.,The Cython compiler, https://cython.org/.
[IronPython]Jim Hugunin et al., https://archive.codeplex.com/?p=IronPython.
[Jython]J. Huginin, B. Warsaw, F. Bock, et al.,Jython: Python for the Java platform, http://www.jython.org.
[PyPy]The PyPy Group, PyPy: a Python implementation written in Python,https://pypy.org/.
[Pyrex]G. Ewing, Pyrex: C-Extensions for Python,https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
[Python]G. van Rossum et al., The Python programming language,https://www.python.org/.