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.
#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.
Dev Digest 205: AI vs. OSS, Hidden ChatGPT Features, Linux in a PDFInside last week’s Dev Digest 205 .
😔 The end of the curl bug bounty
📝 Agent Skills vs. Rules vs. Commands
💬 The best hidden ChatGPT features
📅 Weaponising calendar invites
🟪 CSS in 2026
🐍 Python numbers you should know
👨💻 The Github Copilot SDK
💻 ...
Benedikt Bischof
PyLadies ViennaWhat is Pyladies Vienna all about?Pyladies is an international movement that aims to encourage more women to get into IT. There are many local chapters and the organization is still growing. Even though Vienna and Austria are modern there is still ...