Talk: Part II: Python and C++ Integration with Boost.Python (Workshop)
Python and C++ are both popular languages that each bring a lot to the table. The languages also complement one another well: Python is high-level, dynamic, and easy to use while C++ is at-the-metal, static, and (in)famously tricky. There are times when there are real advantages to combining these disparate natures, and Python’s C API provides a strong interface for doing just that. Boost.Python is a C++ library that builds upon and improves Python’s C API to give users a simpler, more intuitive, and safer means to integrate Python and C++.
In this workshop you’ll learn how to use Boost.Python to effectively bridge the Python/C++ boundary. We’ll start by briefly looking at the fundamentals of the Python C API since that defines the “ground rules”; this includes things like reference counting, the basic object model, and so forth. We’ll then take a look at the Boost.Python API and show how it provides the same functionality as the underlying C API, but does so in a way that doesn’t obscure the real semantics of the Python language.
This is a hands-on workshop where you’ll be doing programming, so bring a laptop. You can work alone or in a group, but be prepared to write some code. To prepare your system for the workshop, please use this system test project:
For the workshop itself, please download and unpack this zip file containing the exercises: https://goo.gl/aT2VH4
Specific topics in this talk will include:
- Exposing classes across the language boundary
- Proper exception handling techniques
- Module initialization
- Packaging and code structure
- Debugging and profiling
- Negotiating the very different type system
- Compilation and linking requirements
We’ll look at using Boost.Python for both extending Python (using C++ code in Python) and embedding it (using Python code from C++.) Audience members will learn enough to do fairly sophisticated Python/C++ integration right away, they’ll see how easy it is to use Python rather than “greenspunning” their own language, and they’ll get the orientation they need in order to learn the rest of what there is to know.
At the end we’ll look at a specific case study in which Boost.Python was used to improve the performance of some Python functionality by a factor of 10. This example is useful not only because it shows how we can play to the strengths of C++ and Python, but it shows how we need to think about the “inter-language barrier” to do so harmoniously.