Cython 3.0: The next generation of Python at the speed of C


Cython, the Python library for compiling Python code to C, has at last released version 3.0 after many years of development and refinement.

The main goal behind Cython is to make it easier to write C extensions for Python, whether for speed or to author convenient interfaces to C libraries. In many ways Cython 3 cleans up and modernizes Cython. It drops support for the long-obsolete Python 2, adds support for newer Python features (up to Python 3.12), and expands the use of “pure Python mode.”

Pure Python mode allows Python developers to use their existing Python linting and code analysis tools on Cython. Historically, Cython used its own peculiar syntax, a hybrid of Python syntax and the C type declaration syntax, which made Cython hard to troubleshoot with Python tooling. Over time Cython began offering an alternative syntax that was fully compatible with conventional Python syntax, called pure Python mode. The vast majority of Cython functions are now exposed in pure Python mode, including functions for calling external C libraries.

Another major area of improvement is NumPy support. Cython has long played well with NumPy, allowing you to write Cython functions that hook directly and natively into NumPy functions and data structures. Cython 3 adds (among other things) the ability to write NumPy ufuncs directly in Cython, so that a simple numerical function written in Cython can be quickly and easily applied to the entire contents of a NumPy data structure.

Cython’s internals have also been reworked to better complement the ongoing changes to Python’s internals. The new “limited API” for Python, for instance, exposes a guaranteed stable subset of Python’s APIs, specifically for the kind of work Cython often does to hook into the Python interpreter. Cython 3 has preliminary but growing support for the limited API. In the long run, this means Cython extension modules built for one version of Python will work in future versions of Python without needing to be recompiled.

The first releases of Cython 3 emerged alongside Python 3.8 three years ago. No firm date or version target was set for when Cython 3 would be finalized. But Cython’s development team encouraged wide use of the alpha and beta versions of Cython 3, and the project has kept pace with new features and internal changes in every version of Python along the way.

Copyright © 2023 IDG Communications, Inc.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *