Serdar Yegulalp
Senior Writer

Cython 0.27 speeds Python by moving away from oddball syntax

news
Sep 25, 20172 mins

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

Python:Programming made easy
Credit: Thinkstock

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: cython.int = 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.

Serdar Yegulalp

Serdar Yegulalp is a senior writer at InfoWorld. A veteran technology journalist, Serdar has been writing about computers, operating systems, databases, programming, and other information technology topics for 30 years. Before joining InfoWorld in 2013, Serdar wrote for Windows Magazine, InformationWeek, Byte, and a slew of other publications. At InfoWorld, Serdar has covered software development, devops, containerization, machine learning, and artificial intelligence, winning several B2B journalism awards including a 2024 Neal Award and a 2025 Azbee Award for best instructional content and best how-to article, respectively. He currently focuses on software development tools and technologies and major programming languages including Python, Rust, Go, Zig, and Wasm. Tune into his weekly Dev with Serdar videos for programming tips and techniques and close looks at programming libraries and tools.

More from this author