Book Image

Learning Cython Programming (Second Edition) - Second Edition

By : Philip Herron
Book Image

Learning Cython Programming (Second Edition) - Second Edition

By: Philip Herron

Overview of this book

Cython is a hybrid programming language used to write C extensions for Python language. Combining the practicality of Python and speed and ease of the C language it’s an exciting language worth learning if you want to build fast applications with ease. This new edition of Learning Cython Programming shows you how to get started, taking you through the fundamentals so you can begin to experience its unique powers. You’ll find out how to get set up, before exploring the relationship between Python and Cython. You’ll also look at debugging Cython, before moving on to C++ constructs, Caveat on C++ usage, Python threading and GIL in Cython. Finally, you’ll learn object initialization and compile time, and gain a deeper insight into Python 3, which will help you not only become a confident Cython developer, but a much more fluent Python developer too.
Table of Contents (14 chapters)
Learning Cython Programming Second Edition
About the Author
About the Reviewer

Cython PXD

The use of PXD files is very similar to that of header files in C/C++. When writing bindings to any C/C++ code, it is a good practice to declare all C/C++ interfaces within a .pxd file. This stands for Python External Declarations, at least it does in my mind. So, when we add blocks such as this:

cdef extern from "AddFunction.h":
    cdef int AddFunction(int, int)

We can instead put this directly into a bindings.pxd file and import this at any time inside any .pyx file:

cimport bindings

Notice the distinction between cimport for the .pxd files and a simple import for all normal Python imports.


Cython's input filenames cannot handle dashes (-) in their filenames. It's best to try and use CamelCase, since you can't use cimport my-import in Python.