Tom Ritchford
2 min readSep 14, 2019

--

This isn’t really true.

First, you can compile your Python code without any type information and you will still get a speedup.

Second, when you add the cdef you aren’t just “adding type information” — you’re writing in a new language that is like Python but isn’t Python.

In fact, Python already has type annotations in the language — and these are incompatible with Cython’s typing. (There have been long discussions of this on the Cython mailing list, and the summary is that Python’s annotations are not a good fit for Cython’s declarations, which are essentially “C++ declarations in Python”.)

I did a medium-sized project with Cython and I got good results with it. I would no longer recommend it most of the time.

The issue is that you end up in practice with three different codebases:

  1. Python
  2. Cython
  3. C++

and two layers of tooling:

  1. Cython ->C++
  2. C++->compiled code

The C++ layer becomes essential in any non-toy project because it’s expensive and clumsy to go back and forth between Python data structures and “raw” C/C++/machine language data structures. Reading the C code generated by Cython shows you why this is necessary.

In particular, the tooling for Cython is not good. Debugging is painful, though achievable. It’s enough hassle that you tend to rely on print statements. Most editors don’t understand Cython — I ended up doing it all in Emacs, which does, but that might not be good for most people. You need to build a makefile (or CMake, etc) and such tedium.

These days, I would probably recommend using only two languages: Python and C++. I’d test the C++ code in C++, the Python code in Python, and use pybind11 to glue them together.

Then you simply don’t have to learn all the details of Cython, which I spent probably one hundred hours on over the project if you include

Don’t get me wrong — I loved Cython at the time, it was very satisfactory for my needs, and I still have warm memories of it. There are still reasons it might be better for your specific project, and my recommendation above is for the general case only.

--

--

Responses (1)