Dedication
For Ellen, Zlie, Leo, and Hughmy muses.
An inconvenience is only an adventure wrongly considered; an adventure is aninconvenience rightly considered.
GKC
A dead thing can go with the stream, but only a living thing can go againstit.
GKC
Preface
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless youre Dutch.
T. PetersThe Zen of Python
Cython: A Guide for Python Programmers covers all you need to know about theeponymous creole programming language and Python-to-C compiler. If you haveheard of Cython and want to find out more, or if you have been using Cython andwant to go deeper, then this book is for you.
Cython is not another experimental (and all too often minimally maintained)language xtolanguage y compiler project. Neither is it limited to aninteresting research project that never achieves widespread use. Cython is anintegral part of foundational projects in the Python world. It is battle-tested in real-world environments, and it continues to innovate to providebetter performance, greater ease of use, and better coverage of new Pythonfeatures.
Who Should Read This Book?
This book is for you if:
- While programming Python, you have thought, These nested
for
loops would run hundreds of times faster in C, but the hassle isnt worth it. - You have considered using PyPy, Numba, or even Julia but want something more mature and with better support tools.
- You have ever wished Python supported optional static typing to speed up the numeric expression that takes up 40 percent of your runtime.
- You use NumPy, SciPy, Pandas, a scikit, or some other data-intensive package and want to go beyond the prepackaged algorithms without compromising performance.
- You have a tested and optimized C or C++ library that you want to wrap with Python without learning the arcana of yet another interfacing language.
- You have considered reprogramming that performance-critical part of your Python application as an extension module but were (rightly) put off by all the fussy details.
Prerequisites
Cython is unique in that it exists between languages. It is a hybrid, achimera, a saber-toothed moose lion. Cython is mostly Python and comes froma Python frame of mind, so this book assumes an intermediate level of Pythonexperience. You should be comfortable with all built-in data types, functions,classes, Pythons object model, modules, packages, and the more common packagesin the standard library. Knowing a bitor willingness to learnabout howCPython works under the hood is helpful as well.
Intermediate experience with NumPy is assumed for later chapters.
Cython also speaks C, so at least a beginners level of knowledge of the C orC++ language is necessary. Familiarity with the built-in C numerictypes, pointers, C arrays, structs, unions, enums, and macros is useful.Cython takes a lot of the scariness and danger out of programming in C, but togo really far, the more C knowledge you have, the better. The C andC++ wrapping chapters assume an intermediate level of familiaritywith these languages and are self-contained.
Who Should Not Read This Book?
If you are just starting out in Python, you will likely benefit fromprogramming a few stretch projects before diving in here.
If you have had no exposure to C or C++, then you will likely need tohave reference material handy to help you understand the C- and C++-specific parts. Going through a C or C++ tutorial and having somefamiliarity with compiled languages will serve you well.
Outline
Most of this book is written in a combination tutorial/reference style. Most chaptersare meant to be read more or less in succession and will often build onpreviously covered material and concepts:
The whirlwind tour, the 50,000-foot view: come here to marvel at how effortless Cython makes speeding up Python and interfacing with C.Where we get you up and running so you can use Cython in your projects.Where we come to understand how Cython can speed up Python by several orders of magnitude. We also go into the basic elements of the Cython language, and what they do.The first of our practice chapters. We start with a pure-Python program that simulates the solar system and use what we have learned so far to speed it up by two orders of magnitude.Where we learn how to create new Python types with Cython and see just how fast OOP in Python can be.Where we learn about Cythons definition files; implementation files; and how to create, organize, and work with Cython projects, small and large.The first wrapping chapter: this covers the basic wrapping concepts and how to wrap a C library with Cython. Users will never know there is a C library underneath that beautiful Python interface!Where we go down the rabbit hole of interfacing with C++, and see how Cython makes easy things simple and hard things possible.Where we learn about Cythons runtime and compile-time profiling tools, and how to use them to help optimize our Cython code.Where we learn all about Cythons support for efficient array-oriented operations, and how to achieve truly massive performance improvements over Python.Our second practice chapter. This time we focus on optimizing a straightforward but nontrivial array-centric program, and achieve performance on par with a pure-C version.Where we discover Cythons prange
special function, which allows us to easily turn on thread-based parallelism and bypass the global interpreter lock.Where we compare Cython with other tools in the same space and indulge in a little prognostication.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and fileextensions. Constant width
Used for program listings, as well as within paragraphs torefer to program elements such as variable or function names, datatypes, statements, and keywords. Constant width italic
Shows text that should be replaced withuser-supplied values or by values determined by context.
Tip
This element signifies a tip or suggestion.
Note
This element signifies a general note.
Warning
This element indicates a warning or caution.
Using Code Examples
Supplemental material and the full source code for the in-text examples isavailable for download at https://github.com/cythonbook/examples.
All Cython code in this book is tested with Cython versions 0.20.2 and 0.21.The Cython language and compiler are fairly stable, and the code in this bookwill likely work with several earlier and later versions. That said, there iscurrently no strong backward compatibility constraint for future Cythonreleases, so some examples may require updating in the future.
This book is here to help you get your job done. In general, if example code isoffered with this book, you may use it in your programs and documentation. Youdo not need to contact us for permission unless youre reproducing asignificant portion of the code. For example, writing a program that usesseveral chunks of code from this book does not require permission. Selling ordistributing a CD-ROM of examples from OReilly books does require permission.Answering a question by citing this book and quoting example code does notrequire permission. Incorporating a significant amount of example code fromthis book into your products documentation does require permission.
Next page