简介
Contents
pybind11 —— C++ 11 和 Python 之间的无缝可操作性
Setuptools example • Scikit-build example • CMake example
pybind11 是轻量级的头文件库,它在 Python 中公开 C++ 类型,反之亦然,主要用于创建现有 C++ 代码的 Python 绑定。它的目标和语法与出色的 Boost.Python 库相似,作者 David Abrahams:通过使用编译时内省推断类型信息来最小化传统扩展模块中的样板代码。
创建类似项目的原因是 Boost.Python 的主要问题是非常大且复杂的实用程序库套件,几乎可以与现有的每一个 C++ 编译器一起工作。这种兼容性也有其代价:晦涩的模板技巧和变通方法是支持最古老和 bug 最多的编译器样本所必需的。现在兼容 C++ 11 的编译器已经被广泛使用,这个笨重的机器已经成为了过于庞大和不必要的依赖。
可以将这个库看作 Boost 的小型的自包含版本。删除与绑定生成无关的所有内容。如果没有注释,核心头文件只需要大约 4K 行代码,并且依赖于 Python(3.6+ 或 PyPy)和 C++ 标准库。这种紧凑的实现得益于一些新的 C++ 11 语言特性(特别是元组、lambda 函数和可变参数模板)。自创建以来,这个库已经超越了 Boost。在许多常见情况下,使用 Python 会大大简化绑定代码。
pybind11.readthedocs.io 提供了教程和参考文档。该手册的 PDF 版本可以在这里找到。源代码可以在 github.com/pybind/pybind11。
核心特性#
pybind11 可以将以下 C++ 核心特性映射到 Python:
函数接受并返回自定义数据结构的每个值、引用或指针
实例方法和静态方法
重载函数
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 extended in Python
Goodies#
In addition to the core functionality, pybind11 provides some extra goodies:
Python 3.6+, and PyPy3 7.3 are supported with an implementation-agnostic interface (pybind11 2.9 was the last version to support Python 2 and 3.5).
It is possible to bind C++11 lambda functions with captured variables. The lambda capture data is stored inside the resulting Python function object.
pybind11 uses C++11 move constructors and move assignment operators whenever possible to efficiently transfer custom data types.
It’s easy to expose the internal storage of custom data types through Pythons’ buffer protocols. This is handy e.g. for fast conversion between C++ matrix classes like Eigen and NumPy without expensive copy operations.
pybind11 can automatically vectorize functions so that they are transparently applied to all entries of one or more NumPy array arguments.
Python’s slice-based access and assignment operations can be supported with just a few lines of code.
Everything is contained in just a few header files; there is no need to link against any additional libraries.
Binaries are generally smaller by a factor of at least 2 compared to equivalent bindings generated by Boost.Python. A recent pybind11 conversion of PyRosetta, an enormous Boost.Python binding project, reported a binary size reduction of 5.4x and compile time reduction by 5.8x.
Function signatures are precomputed at compile time (using
constexpr
), leading to smaller binaries.With little extra effort, C++ types can be pickled and unpickled similar to regular Python objects.
Supported compilers#
Clang/LLVM 3.3 or newer (for Apple Xcode’s clang, this is 5.0.0 or newer)
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)
About#
This project was created by Wenzel Jakob. Significant features and/or improvements to the code were contributed by Jonas 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, Boris Staletic, and Patrick Stewart.
We thank Google for a generous financial contribution to the continuous integration infrastructure used by this project.
Contributing#
See the contributing guide for information on building and contributing to pybind11.
License#
pybind11 is provided under a BSD-style license that can be found in the LICENSE file. By using, distributing, or contributing to this project, you agree to the terms and conditions of this license.
Contents:
- Functions
- Return value policies
- Additional call policies
- Python objects as arguments
- Accepting *args and **kwargs
- Default arguments revisited
- Keyword-only arguments
- Positional-only arguments
- Non-converting arguments
- Allow/Prohibiting None arguments
- Overload resolution order
- Binding functions with template parameters
- Classes
- Overriding virtual functions in Python
- Combining virtual functions and inheritance
- Extended trampoline class functionality
- Custom constructors
- Non-public destructors
- Destructors that call Python
- Implicit conversions
- Static properties
- Operator overloading
- Pickling support
- Deepcopy support
- Multiple Inheritance
- Module-local class bindings
- Binding protected member functions
- Binding final classes
- Binding classes with template parameters
- Custom automatic downcasters
- Accessing the type object
- Custom type setup
- Exceptions
- Smart pointers
- Type conversions
- Python C++ 接口
- Embedding the interpreter
- Miscellaneous
- General notes regarding convenience macros
- Global Interpreter Lock (GIL)
- Common Sources Of Global Interpreter Lock Errors
- Binding sequence data types, iterators, the slicing protocol, etc.
- Partitioning code over multiple extension modules
- Module Destructors
- Generating documentation using Sphinx
- Avoiding C++ types in docstrings