|
Packit |
534379 |
.. image:: pybind11-logo.png
|
|
Packit |
534379 |
|
|
Packit |
534379 |
About this project
|
|
Packit |
534379 |
==================
|
|
Packit |
534379 |
**pybind11** is a lightweight header-only library that exposes C++ types in Python
|
|
Packit |
534379 |
and vice versa, mainly to create Python bindings of existing C++ code. Its
|
|
Packit |
534379 |
goals and syntax are similar to the excellent `Boost.Python`_ library by David
|
|
Packit |
534379 |
Abrahams: to minimize boilerplate code in traditional extension modules by
|
|
Packit |
534379 |
inferring type information using compile-time introspection.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
.. _Boost.Python: http://www.boost.org/doc/libs/release/libs/python/doc/index.html
|
|
Packit |
534379 |
|
|
Packit |
534379 |
The main issue with Boost.Python—and the reason for creating such a similar
|
|
Packit |
534379 |
project—is Boost. Boost is an enormously large and complex suite of utility
|
|
Packit |
534379 |
libraries that works with almost every C++ compiler in existence. This
|
|
Packit |
534379 |
compatibility has its cost: arcane template tricks and workarounds are
|
|
Packit |
534379 |
necessary to support the oldest and buggiest of compiler specimens. Now that
|
|
Packit |
534379 |
C++11-compatible compilers are widely available, this heavy machinery has
|
|
Packit |
534379 |
become an excessively large and unnecessary dependency.
|
|
Packit |
534379 |
Think of this library as a tiny self-contained version of Boost.Python with
|
|
Packit |
534379 |
everything stripped away that isn't relevant for binding generation. Without
|
|
Packit |
534379 |
comments, the core header files only require ~4K lines of code and depend on
|
|
Packit |
534379 |
Python (2.7 or 3.x, or PyPy2.7 >= 5.7) and the C++ standard library. This
|
|
Packit |
534379 |
compact implementation was possible thanks to some of the new C++11 language
|
|
Packit |
534379 |
features (specifically: tuples, lambda functions and variadic templates). Since
|
|
Packit |
534379 |
its creation, this library has grown beyond Boost.Python in many ways, leading
|
|
Packit |
534379 |
to dramatically simpler binding code in many common situations.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Core features
|
|
Packit |
534379 |
*************
|
|
Packit |
534379 |
The following core C++ features can be mapped to Python
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Functions accepting and returning custom data structures per value, reference, or pointer
|
|
Packit |
534379 |
- Instance methods and static methods
|
|
Packit |
534379 |
- Overloaded functions
|
|
Packit |
534379 |
- Instance attributes and static attributes
|
|
Packit |
534379 |
- Arbitrary exception types
|
|
Packit |
534379 |
- Enumerations
|
|
Packit |
534379 |
- Callbacks
|
|
Packit |
534379 |
- Iterators and ranges
|
|
Packit |
534379 |
- Custom operators
|
|
Packit |
534379 |
- Single and multiple inheritance
|
|
Packit |
534379 |
- STL data structures
|
|
Packit |
534379 |
- Smart pointers with reference counting like ``std::shared_ptr``
|
|
Packit |
534379 |
- Internal references with correct reference counting
|
|
Packit |
534379 |
- C++ classes with virtual (and pure virtual) methods can be extended in Python
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Goodies
|
|
Packit |
534379 |
*******
|
|
Packit |
534379 |
In addition to the core functionality, pybind11 provides some extra goodies:
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) are supported with an
|
|
Packit |
534379 |
implementation-agnostic interface.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- It is possible to bind C++11 lambda functions with captured variables. The
|
|
Packit |
534379 |
lambda capture data is stored inside the resulting Python function object.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- pybind11 uses C++11 move constructors and move assignment operators whenever
|
|
Packit |
534379 |
possible to efficiently transfer custom data types.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- It's easy to expose the internal storage of custom data types through
|
|
Packit |
534379 |
Pythons' buffer protocols. This is handy e.g. for fast conversion between
|
|
Packit |
534379 |
C++ matrix classes like Eigen and NumPy without expensive copy operations.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- pybind11 can automatically vectorize functions so that they are transparently
|
|
Packit |
534379 |
applied to all entries of one or more NumPy array arguments.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Python's slice-based access and assignment operations can be supported with
|
|
Packit |
534379 |
just a few lines of code.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Everything is contained in just a few header files; there is no need to link
|
|
Packit |
534379 |
against any additional libraries.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Binaries are generally smaller by a factor of at least 2 compared to
|
|
Packit |
534379 |
equivalent bindings generated by Boost.Python. A recent pybind11 conversion
|
|
Packit |
534379 |
of `PyRosetta`_, an enormous Boost.Python binding project, reported a binary
|
|
Packit |
534379 |
size reduction of **5.4x** and compile time reduction by **5.8x**.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Function signatures are precomputed at compile time (using ``constexpr``),
|
|
Packit |
534379 |
leading to smaller binaries.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- With little extra effort, C++ types can be pickled and unpickled similar to
|
|
Packit |
534379 |
regular Python objects.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
.. _PyRosetta: http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Supported compilers
|
|
Packit |
534379 |
*******************
|
|
Packit |
534379 |
|
|
Packit |
534379 |
1. Clang/LLVM (any non-ancient version with C++11 support)
|
|
Packit |
534379 |
2. GCC 4.8 or newer
|
|
Packit |
534379 |
3. Microsoft Visual Studio 2015 or newer
|
|
Packit |
534379 |
4. Intel C++ compiler v17 or newer (v16 with pybind11 v2.0 and v15 with pybind11 v2.0 and a `workaround <https://github.com/pybind/pybind11/issues/276>`_ )
|