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

SWIG and Cython

Overall, if you consider SWIG ( as a way to write a native Python module, you could be fooled to think that Cython and SWIG are similar. SWIG is mainly used to write wrappers for language bindings. For example, if you have some C code as follows:

int myFunction (int, const char *){ … }

You can write the SWIG interface file as follows:

/* example.i */
%module example
  extern int myFunction (int, const char *);

Compile this with the following:

$ swig -python example.i

You can compile and link the module as you would do for a Cython output since this generates the necessary C code. This is fine if you want a basic module to simply call into C from Python. But Cython provides users with much more.

Cython is much more developed and optimized, and it truly understands how to work with C types and memory management and how to handle exceptions. With SWIG, you cannot manipulate data; you simply call into functions on the C side from Python. In Cython, we can...