- Notifications
You must be signed in to change notification settings - Fork2.1k
Seamless operability between C++11 and Python
License
pybind/pybind11
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pybind11 (v3) — Seamless interoperability between C++ and Python
Setuptools example•Scikit-build example•CMake example
pybind11 is a lightweight header-only library that exposes C++ typesin Python and vice versa, mainly to create Python bindings of existingC++ code. Its goals and syntax are similar to the excellentBoost.Pythonlibrary by David Abrahams: to minimize boilerplate code in traditionalextension modules by inferring type information using compile-timeintrospection.
The main issue with Boost.Python—and the reason for creating such asimilar project—is Boost. Boost is an enormously large and complex suiteof utility libraries that works with almost every C++ compiler inexistence. This compatibility has its cost: arcane template tricks andworkarounds are necessary to support the oldest and buggiest of compilerspecimens. Now that C++11-compatible compilers are widely available,this heavy machinery has become an excessively large and unnecessarydependency.
Think of this library as a tiny self-contained version of Boost.Pythonwith everything stripped away that isn't relevant for bindinggeneration. Without comments, the core header files only require ~4Klines of code and depend on Python (3.8+, or PyPy) and the C++standard library. This compact implementation was possible thanks tosome C++11 language features (specifically: tuples, lambda functions andvariadic templates). Since its creation, this library has grown beyondBoost.Python in many ways, leading to dramatically simpler binding code in manycommon situations.
Tutorial and reference documentation is provided atpybind11.readthedocs.io.A PDF version of the manual is availablehere.And the source code is always available atgithub.com/pybind/pybind11.
pybind11 can map the following core C++ features to Python:
- Functions accepting and returning custom data structures per value,reference, or pointer
- Instance methods and static methods
- Overloaded functions
- Instance attributes and static attributes
- Arbitrary exception types
- Enumerations
- Callbacks
- Iterators and ranges
- Custom operators
- Single and multiple inheritance
- STL data structures
- Smart pointers with reference counting like
std::shared_ptr
- Internal references with correct reference counting
- C++ classes with virtual (and pure virtual) methods can be extendedin Python
- Integrated NumPy support (NumPy 2 requires pybind11 2.12+)
In addition to the core functionality, pybind11 provides some extragoodies:
- Python 3.8+, and PyPy3 7.3 are supported with an implementation-agnosticinterface (pybind11 2.9 was the last version to support Python 2 and 3.5).
- It is possible to bind C++11 lambda functions with capturedvariables. The lambda capture data is stored inside the resultingPython function object.
- pybind11 uses C++11 move constructors and move assignment operatorswhenever possible to efficiently transfer custom data types.
- It's easy to expose the internal storage of custom data types throughPythons' buffer protocols. This is handy e.g. for fast conversionbetween C++ matrix classes like Eigen and NumPy without expensivecopy operations.
- pybind11 can automatically vectorize functions so that they aretransparently applied to all entries of one or more NumPy arrayarguments.
- Python's slice-based access and assignment operations can besupported with just a few lines of code.
- Everything is contained in just a few header files; there is no needto link against any additional libraries.
- Binaries are generally smaller by a factor of at least 2 compared toequivalent bindings generated by Boost.Python. A recent pybind11conversion of PyRosetta, an enormous Boost.Python binding project,reporteda binary size reduction of5.4x and compile time reduction by5.8x.
- Function signatures are precomputed at compile time (using
constexpr
), leading to smaller binaries. - With little extra effort, C++ types can be pickled and unpickledsimilar to regular Python objects.
- Clang/LLVM 3.3 or newer (for Apple Xcode's clang, this is 5.0.0 ornewer)
- GCC 4.8 or newer
- Microsoft Visual Studio 2017 or newer
- Intel classic C++ compiler 18 or newer (ICC 20.2 tested in CI)
- Cygwin/GCC (previously tested on 2.5.1)
- NVCC (CUDA 11.0 tested in CI)
- NVIDIA PGI (20.9 tested in CI)
This project was created byWenzelJakob. Significant features and/orimprovements to the code were contributed byJonas Adler,Lori A. Burns,Sylvain Corlay,Eric Cousineau,Aaron Gokaslan,Ralf Grosse-Kunstleve,Trent Houliston,Axel Huebl,@hulucc,Yannick Jadoul,Sergey Lyskov,Johan Mabille,Tomasz Miąsko,Dean Moldovan,Ben Pritchard,Jason Rhinelander,Boris Schäling,Pim Schellart,Henry Schreiner,Ivan Smirnov,Dustin Spicuzza,Boris Staletic,Ethan Steinberg,Patrick Stewart,Ivor Wanders,andXiaofei Wang.
We thank Google for a generous financial contribution to the continuousintegration infrastructure used by this project.
See thecontributingguidefor information on building and contributing to pybind11.
pybind11 is provided under a BSD-style license that can be found in theLICENSEfile. By using, distributing, or contributing to this project, you agreeto the terms and conditions of this license.
About
Seamless operability between C++11 and Python