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