blob: f802d7dea20925aafcbf3defcdd37ac05cbd8e6a [file] [log] [blame] [view]
Wenzel Jakob10d992e2015-08-04 13:59:51 +02001![pybind11 logo](https://github.com/wjakob/pybind11/raw/master/logo.png)
2
3# pybind11 — Seamless operability between C++11 and Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
5**pybind11** is a lightweight header library that exposes C++ types in Python
6and vice versa, mainly to create Python bindings of existing C++ code. Its
7goals and syntax are similar to the excellent
8[Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library
9by David Abrahams: to minimize boilerplate code in traditional extension
10modules by inferring type information using compile-time introspection.
11
12The main issue with Boost.Python—and the reason for creating such a similar
13project—is Boost. Boost is an enormously large and complex suite of utility
14libraries that works with almost every C++ compiler in existence. This
15compatibility has its cost: arcane template tricks and workarounds are
16necessary to support the oldest and buggiest of compiler specimens. Now that
17C++11-compatible compilers are widely available, this heavy machinery has
18become an excessively large and unnecessary dependency.
19
20Think of this library as a tiny self-contained version of Boost.Python with
21everything stripped away that isn't relevant for binding generation. The whole
Wenzel Jakob57082212015-09-04 23:42:12 +020022codebase requires less than 3000 lines of code and only depends on Python (2.7
23or 3.x) and the C++ standard library. This compact implementation was possible
24thanks to some of the new C++11 language features (tuples, lambda functions and
25variadic templates).
Wenzel Jakob38bd7112015-07-05 20:05:44 +020026
27## Core features
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020028The following core C++ features can be mapped to Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020029
30- Functions accepting and returning custom data structures per value, reference, or pointer
31- Instance methods and static methods
32- Overloaded functions
33- Instance attributes and static attributes
34- Exceptions
35- Enumerations
36- Callbacks
37- Custom operators
38- STL data structures
39- Smart pointers with reference counting like `std::shared_ptr`
40- Internal references with correct reference counting
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020041- C++ classes with virtual (and pure virtual) methods can be extended in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020042
43## Goodies
44In addition to the core functionality, pybind11 provides some extra goodies:
45
46- It's easy to expose the internal storage of custom data types through
47 Pythons' buffer protocols. This is handy e.g. for fast conversion between
48 C++ matrix classes like Eigen and NumPy without expensive copy operations.
49
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020050- pybind11 can automatically vectorize functions so that they are transparently
51 applied to all entries of one or more NumPy array arguments.
52
Wenzel Jakob38bd7112015-07-05 20:05:44 +020053- Python's slice-based access and assignment operations can be supported with
54 just a few lines of code.
55
56- pybind11 uses C++11 move constructors and move assignment operators whenever
57 possible to efficiently transfer custom data types.
58
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020059- It is possible to bind C++11 lambda functions with captured variables. The
60 lambda capture data is stored inside the resulting Python function object.
61
Wenzel Jakob38bd7112015-07-05 20:05:44 +020062## What does the binding code look like?
63Here is a simple example. The directory `example` contains many more.
64```C++
65#include <pybind/pybind.h>
66#include <pybind/operators.h>
67
68namespace py = pybind;
69
70/// Example C++ class which should be bound to Python
71class Test {
72public:
73 Test();
74 Test(int value);
75 std::string toString();
76 Test operator+(const Test &e) const;
77
78 void print_dict(py::dict dict) {
79 /* Easily interact with Python types */
80 for (auto item : dict)
81 std::cout << "key=" << item.first << ", "
82 << "value=" << item.second << std::endl;
83 }
84
85 int value = 0;
86};
87
88
89PYTHON_PLUGIN(example) {
90 py::module m("example", "pybind example plugin");
91
92 py::class_<Test>(m, "Test", "docstring for the Test class")
93 .def(py::init<>(), "docstring for constructor 1")
94 .def(py::init<int>(), "docstring for constructor 2")
95 .def(py::self + py::self, "Addition operator")
96 .def("__str__", &Test::toString, "Convert to a string representation")
97 .def("print_dict", &Test::print_dict, "Print a Python dictionary")
98 .def_readwrite("value", &Test::value, "An instance attribute");
99
100 return m.ptr();
101}
102```
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200103
104## A collection of specific use cases (mostly buffer-related for now)
105For brevity, let's set
106```C++
107namespace py = pybind;
108```
109### Exposing buffer views
110Python supports an extremely general and convenient approach for exchanging
111data between plugin libraries. Types can expose a buffer view which provides
112fast direct access to the raw internal representation. Suppose we want to bind
113the following simplistic Matrix class:
114
115```C++
116class Matrix {
117public:
118 Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
119 m_data = new float[rows*cols];
120 }
121 float *data() { return m_data; }
122 size_t rows() const { return m_rows; }
123 size_t cols() const { return m_cols; }
124private:
125 size_t m_rows, m_cols;
126 float *m_data;
127};
128```
129The following binding code exposes the ``Matrix`` contents as a buffer object,
130making it possible to cast Matrixes into NumPy arrays. It is even possible to
131completely avoid copy operations with Python expressions like
132``np.array(matrix_instance, copy = False)``.
133```C++
134py::class_<Matrix>(m, "Matrix")
135 .def_buffer([](Matrix &m) -> py::buffer_info {
136 return py::buffer_info(
137 m.data(), /* Pointer to buffer */
138 sizeof(float), /* Size of one scalar */
139 py::format_descriptor<float>::value(), /* Python struct-style format descriptor */
140 2, /* Number of dimensions */
141 { m.rows(), m.cols() }, /* Buffer dimensions */
142 { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
143 sizeof(float) }
144 );
145 });
146```
147The snippet above binds a lambda function, which can create ``py::buffer_info``
148description records on demand describing a given matrix. The contents of
149``py::buffer_info`` mirror the Python buffer protocol specification.
150```C++
151struct buffer_info {
152 void *ptr;
153 size_t itemsize;
154 std::string format;
155 int ndim;
156 std::vector<size_t> shape;
157 std::vector<size_t> strides;
158};
159```
160### Taking Python buffer objects as arguments
161To create a C++ function that can take a Python buffer object as an argument,
162simply use the type ``py::buffer`` as one of its arguments. Buffers can exist
163in a great variety of configurations, hence some safety checks are usually
164necessary in the function body. Below, you can see an basic example on how to
165define a custom constructor for the Eigen double precision matrix
166(``Eigen::MatrixXd``) type, which supports initialization from compatible
167buffer
168objects (e.g. a NumPy matrix).
169```C++
170py::class_<Eigen::MatrixXd>(m, "MatrixXd")
171 .def("__init__", [](Eigen::MatrixXd &m, py::buffer b) {
172 /* Request a buffer descriptor from Python */
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200173 py::buffer_info info = b.request();
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200174
175 /* Some sanity checks ... */
176 if (info.format != py::format_descriptor<double>::value())
177 throw std::runtime_error("Incompatible format: expected a double array!");
178
179 if (info.ndim != 2)
180 throw std::runtime_error("Incompatible buffer dimension!");
181
182 if (info.strides[0] == sizeof(double)) {
183 /* Buffer has the right layout -- directly copy. */
184 new (&m) Eigen::MatrixXd(info.shape[0], info.shape[1]);
185 memcpy(m.data(), info.ptr, sizeof(double) * m.size());
186 } else {
187 /* Oops -- the buffer is transposed */
188 new (&m) Eigen::MatrixXd(info.shape[1], info.shape[0]);
189 memcpy(m.data(), info.ptr, sizeof(double) * m.size());
190 m.transposeInPlace();
191 }
192 });
193```
194
195### Taking NumPy arrays as arguments
196
197By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can
198restrict the function so that it only accepts NumPy arrays (rather than any
199type of Python object satisfying the buffer object protocol).
200
201In many situations, we want to define a function which only accepts a NumPy
202array of a certain data type. This is possible via the ``py::array_dtype<T>``
203template. For instance, the following function requires the argument to be a
204dense array of doubles in C-style ordering.
205```C++
206void f(py::array_dtype<double> array);
207```
208When it is invoked with a different type (e.g. an integer), the binding code
209will attempt to cast the input into a NumPy array of the requested type.
210
211### Auto-vectorizing a function over NumPy array arguments
212Suppose we want to bind a function with the following signature to Python so
213that it can process arbitrary NumPy array arguments (vectors, matrices, general
214N-D arrays) in addition to its normal arguments:
215```C++
216double my_func(int x, float y, double z);
217```
218This is extremely simple to do!
219```C++
220m.def("vectorized_func", py::vectorize(my_func));
221```
222Invoking the function like below causes 4 calls to be made to ``my_func`` with
223each of the the array elements. The result is returned as a NumPy array of type
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200224``numpy.dtype.float64``.
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200225```Python
226>>> x = np.array([[1, 3],[5, 7]])
227>>> y = np.array([[2, 4],[6, 8]])
228>>> z = 3
229>>> result = vectorized_func(x, y, z)
230```
231The scalar argument ``z`` is transparently replicated 4 times. The input
232arrays ``x`` and ``y`` are automatically converted into the right types (they
233are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and
234``numpy.dtype.float32``, respectively)
235
236Sometimes we might want to explitly exclude an argument from the vectorization
237because it makes little sense to wrap it in a NumPy array. For instance,
238suppose the function signature was
239```C++
240double my_func(int x, float y, my_custom_type *z);
241```
242This can be done with a stateful Lambda closure:
243```C++
244// Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
245m.def("vectorized_func",
246 [](py::array_dtype<int> x, py::array_dtype<float> y, my_custom_type *z) {
247 auto stateful_closure = [z](int x, float y) { return my_func(x, y, z); };
248 return py::vectorize(stateful_closure)(x, y);
249 }
250);
251```