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

Integration with build systems

This topic is basically dependent on the linking model that you choose if you are to choose the shared-library approach. I would recommend using Python distutils and if you are going for embedded Python, and if you like GNU or autotools, this section gives an example you can use.

Python Distutils

When compiling a native Python module, we can use distutils and cythonize inside our build. It's the preferred way in Python to use Cython as part of the build:

from distutils.core import setup
from Cython.Build import cythonize

    ext_modules = cythonize("sourcecode.pyx")

This build file will support whichever version of Python you invoke the script with. When you run the build, your output will be of the same name of the input source code as a shared module in this case


To embed Python code within C/C++ applications using the autotools build system the following snippet will help you. It will use python-config to get the...