Cython 0.27 speeds Python by moving away from oddball syntax

The latest version of the Python-to-C toolkit can use Python's own native type-hinting syntax to accelerate the conversion process

Cython 0.27 speeds Python by moving away from oddball syntax

Cython, the toolkit that allows Python code to be converted to high-speed C code, has a new 0.27 release that can now use Python’s own native typing syntax to speed up the Python-to-C conversion process.

Previously, Cython users could accelerate Python only by decorating the code with type annotations in a dialect peculiar to Cython. Python has its own optional syntax for variable type annotation, but Cython didn’t use it.

With Cython 0.27, Cython can now recognize PEP 526-style type declarations for native Python types, such as str or list. The same syntax can also be used to explicitly define native C types, using declarations like var: = 32.

Existing Python tools—IDEs, code linters, and so on—already use PEP 526 for type checking and code analysis. Cython’s changes make it easier to speed code already decorated with those annotations—and also makes it easier for mainstream Python developers to take advantage of Cython’s power, as opposed to Cython enthusiasts who’ve become familiar with Cython’s oddball native syntax.

Right now, only basic varieties of type declarations are supported, so you can’t use Python type declarations to describe things like C-style structs. But Cython’s existing syntax already provides that, and its syntax isn’t likely to be replaced entirely by PEP-526-style markup anytime soon.

Also new in Cython 0.27: Python’s asynchronous generators and comprehensions are now translated by Cython into C. This way, the recently introduced async/await constructions in Python can be accelerated. Note that these features work best when Cython is used with Python 3.6 or better, because they need the support of the Python runtime to work correctly.

If you use Cython with IPython/Jupyter notebooks, the new %%cython —pgo option uses profile-guided optimization to generate a C module that’s been optimized based on its runtime profile. However, this feature is available to use only using the GCC compiler; if you use MSVC as your toolchain, you will have to wait or submit a pull request.

Python has always been, and has always intended to be, a dynamic language, something its creator Guido van Rossum has emphasized in discussions of features like type annotation. But nothing’s stopping other parties from using those type-hinting features to power ahead-of-time compilation, as Cython is now doing.