Konstantin Bespalov

How to implement convenient Python bindings to C++

How do you make a C++ library feel truly Pythonic? Learn to create high-performance bindings with full IDE support, intuitive syntax, and multiprocessing compatibility.

How to implement convenient Python bindings to C++
#1about 4 minutes

The business case for Python bindings to a C++ library

A C++ financial pricing library required a Python interface for quantitative analysts to perform research and development.

#2about 2 minutes

Evaluating an initial approach using .NET integration

The first attempt used a .NET assembly with pythonnet, but it suffered from poor performance and a lack of IDE support.

#3about 4 minutes

Choosing pybind11 for direct C++ to Python bindings

Pybind11 was selected over the Python/C API for its ability to minimize boilerplate code when creating direct C++ bindings.

#4about 2 minutes

Identifying usability gaps in the initial pybind11 bindings

The initial pybind11 implementation still lacked crucial features like type hints, Pythonic collections, and pickling support for multiprocessing.

#5about 2 minutes

Adding IDE support and type hints with stub files

PEP 484 and .pyi stub files provide a type interface for the C++ extension module, enabling autocompletion and navigation in IDEs.

#6about 2 minutes

Creating a Pythonic API with variants and magic methods

Using std::variant simplifies function arguments and implementing magic methods like __iter__ makes C++ collections behave like native Python ones.

#7about 1 minute

Enabling multiprocessing with custom pickling support

The copy_reg module is used to register custom serialization and deserialization functions, making C++ objects picklable for multiprocessing.

#8about 1 minute

Summarizing the solution and using code generation

The final solution combines pybind11 with several enhancements, and a code generator is recommended to maintain the bindings for large projects.

#9about 2 minutes

Q&A on exceptions, SWIG, and code generation tooling

The speaker answers audience questions about propagating C++ exceptions, comparing pybind11 to SWIG, and the specific code generator used.

Related jobs
Jobs that call for the skills explored in this talk.

Featured Partners

From learning to earning

Jobs that call for the skills explored in this talk.