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

Compile time preprocessor

At compile time, similar to C/C++, we have the C-preprocessor to make some decisions on what gets compiled mostly from conditionals, defines, and a mixture of both. In Cython, we can replicate some of this behavior using IF, ELIF, ELSE, and DEF. This is demonstrated as an example in the following code line:

DEF myConstant = "hello cython"

We also have access to os.uname as predefined constants from the Cython compiler:






We can also run conditional expressions against these as follows:

IF UNAME_SYSNAME == "Windows":
    include "windows.pyx"
    include "unix.pyx"

You also have ELIF to use in conditional expressions. If you compare something as this against some of your headers in C programs, you will see how you can replicate basic C-preprocessor behavior in Cython. This gives you a quick idea of how you can replicate C-preprocessor usage in your headers.