In this post, let’s get an introducton to Cython. Specifically What it is and why it really useful?
What is Cython?
Cython helps programmers to boost the performance of code with C-like performance. The developers can load and use the extension modules directly in the Python code through the import statement.
Cython is two closely related things:
- It is a programming language that blends Python with the static type system of C and C++.
- And it is a compiler that translates Cython source code into efficient C or C++ source code. This source can then be compiled into a Python extension module or a standalone executable.
Cython’s power comes from the way it combines Python and C: it feels like Python while providing easy access to C. Cython is situated between high-level Python and low-level C; one might call it a creole programming language.
Both languages are mainstream, but they are typically used in different domains, given their differences. Cython’s beauty is this: it combines Python’s expressiveness and dynamism with C’s bare-metal performance while still feeling like Python.
Cython has expansive goals, first and foremost being full Python compatibility. It has also acquired features that are specific to its unique position between Python and C, making Cython easier to use, more efficient, and more expressive. Some of these Cython-only features are the following:
- 1st one is: the conversion between C types and Python types
- 2ndly the Specialized syntax to ease wrapping and interfacing with C & C++
- The third one is the automatic static type inference for certain code paths
- Another one is: the buffer-specific syntax for First-class buffer support
- And then we have the Typed memoryviews.
- And the last but not least is the Thread-based parallelism using the prange function.
You can find the video for this post here.
Let’s talk about some of the technical things involved in the understanding of Cython.
Cython source file names consist of, name of the module followed by a .pyx extension, for example, a module called primes would have a source file named primes.pyx.
Cython code, unlike Python, must be compiled. This happens in two stages:
- A .pyx file is compiled by Cython to a .c file.
- The .c file is compiled by a C compiler to a .so file (or a .pyd file on Windows)
Once you have written your .pyx file, there are a couple of ways turning it into an extension module. Commonly, there are two ways of compiling from the command line.
- The first one is by using the cython command, which takes a .py or .pyx file and compiles it into a C/C++ file.
- The second one is the cythonize command, which also takes a .py or .pyx file and compiles it into a C/C++ file. But it also compiles the C/C++ files into an extension module which is directly importable from Python.
This post is meant to clarify some of the basics but important concepts. We have seen Cython’s essential features, distilled to their most basic elements. The rest of this Cython series explains the Cython language in-depth, which covers how to compile and run Cython code, how to interface with C and C++, and also provides many examples to help you use Cython effectively in your own projects.