Build software that combines Python’s expressivity with the performance and control of C (and C++). It’s possible with Cython, the compiler and hybrid programming language used by foundational packages such as NumPy, and prominent in projects including Pandas, h5py, and scikits-learn. In this practical guide, you’ll learn how to use Cython to improve Python’s performance—up to 3000x— and to wrap C and C++ libraries in Python with ease.

Author Kurt Smith takes you through Cython’s capabilities, with sample code and in-depth practice exercises. If you’re just starting with Cython, or want to go deeper, you’ll learn how this language is an essential part of any performance-oriented Python programmer’s arsenal.

Kurt W. Smith

Kurt Smith has been using Python in scientific computing ever since his college days, looking for any opportunity to incorporate it into his computational physics classes. He has contributed to the Cython project as part of the 2009 Google Summer of Code, implementing the initial version of typed memoryviews and native cython arrays. He uses Cython extensively in his consulting work at Enthought, training hundreds of scientists, engineers, and researchers in Python, NumPy, Cython, and parallel and high-performance computing.

The animal on the cover of Cython is a South African python (Python sebae natalensis). Also known as the South African rock python or the natal rock python, it was first identified by Sir Andrew Smith in 1833 and is variously labeled a subspecies of or a distinct but closely related species to the African rock pythons native to parts of the African continent farther north. The South African python is found in areas near permanent bodies of water from Kenya to South Africa, and, though generally smaller than its more northern relative, can grow to a length of 20 feet. The subocular mark that appears as a rule on the northern variation is smaller or entirely absent on the South African python.

The nonvenomous South African python regularly consumes animals as large as goats, which it kills by coiling itself around prey and constricting the coil with every inward breath of its victim. As with the heat-sensitive organs between the eyes and nostrils of pit vipers, pits in and around scales on the lips of the African rock python permit these snakes to hunt warm-blooded prey in the dark. Attacks on humans are rare but not unprecedented.

A female South African python lays a clutch of up to 100 hard-shelled eggs in the spring. Like other python mothers, she will then coil herself around the clutch until the eggs hatch 2 to 3 months later. Recent evidence suggests African rock python mothers will even continue to defend the brood for weeks or months after the eggs have hatched.

Like the Burmese python, the African rock python has arrived in recent years as an uninvited guest to the Florida Everglades, where it poses a significant threat to native wildlife. Along with several other decidedly unwelcome nonnative reptile species, it has been targeted by local officials in eradication efforts.

Many of the animals on O'Reilly covers are endangered; all of them are important to the world. To learn more about how you can help, go to animals.oreilly.com.

The cover image is from Wood's Natural History. The cover fonts are URW Typewriter and Guardian Sans. The text font is Adobe Minion Pro; the heading font is Adobe Myriad Condensed; and the code font is Dalton Maag's Ubuntu Mono.

As I'm writing this, the book is still an early release, so I am still missing the last few chapters, but I have been looking forward to, and reading, each update as quickly as they come out.

Python is a great language for math/science work, but as an interpreted language, it can be slow for some uses. Cython is a clever way to overcome this limitation without losing the benefits of Python. This book is a great guide to all aspects of Cython.

You don't need to know any Cython when you start this book (I'd never seen it before), but it is NOT an introduction to Python. I would suggest this book for people who are fairly familiar with Python and want to push it beyond what it can do on its own, or who would like to interface Python with C/C++ code for various reasons.

Beyond just an introduction, this book goes in to great depth on the various aspects of Cython and C/C++ interfacing using Cython. After reading and developing along with this book you should be able to do just about anything there is to do in Cython. I can't recommend it highly enough!