blob: 339d559551f3ed3c81164f8ddbd042c7bfc7aa25 [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _basics:
2
3First steps
4###########
5
6This sections demonstrates the basic features of pybind11. Before getting
7started, make sure that development environment is set up to compile the
Dean Moldovanec0d38e2016-08-13 03:09:52 +02008included set of test cases.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02009
10
11Compiling the test cases
12========================
13
14Linux/MacOS
15-----------
16
17On Linux you'll need to install the **python-dev** or **python3-dev** packages as
18well as **cmake**. On Mac OS, the included python version works out of the box,
19but **cmake** must still be installed.
20
21After installing the prerequisites, run
22
23.. code-block:: bash
24
Dean Moldovanec0d38e2016-08-13 03:09:52 +020025 mkdir build
26 cd build
27 cmake ..
28 make pytest -j 4
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020029
Dean Moldovanec0d38e2016-08-13 03:09:52 +020030The last line will both compile and run the tests.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020031
32Windows
33-------
34
Dean Moldovanec0d38e2016-08-13 03:09:52 +020035On Windows, only **Visual Studio 2015** and newer are supported since pybind11 relies
36on various C++11 language features that break older versions of Visual Studio.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020037
Dean Moldovanec0d38e2016-08-13 03:09:52 +020038To compile and run the tests:
39
40.. code-block:: batch
41
42 mkdir build
43 cd build
44 cmake ..
45 cmake --build . --config Release --target pytest
46
47This will create a Visual Studio project, compile and run the target, all from the
48command line.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020049
50.. Note::
51
Dean Moldovanec0d38e2016-08-13 03:09:52 +020052 If all tests fail, make sure that the Python binary and the testcases are compiled
53 for the same processor type and bitness (i.e. either **i386** or **x86_64**). You
54 can specify **x86_64** as the target architecture for the generated Visual Studio
55 project using ``cmake -A x64 ..``.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020056
57.. seealso::
58
59 Advanced users who are already familiar with Boost.Python may want to skip
Dean Moldovanec0d38e2016-08-13 03:09:52 +020060 the tutorial and look at the test cases in the :file:`tests` directory,
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020061 which exercise all features of pybind11.
62
63Creating bindings for a simple function
64=======================================
65
66Let's start by creating Python bindings for an extremely simple function, which
67adds two numbers and returns their result:
68
69.. code-block:: cpp
70
71 int add(int i, int j) {
72 return i + j;
73 }
74
75For simplicity [#f1]_, we'll put both this function and the binding code into
76a file named :file:`example.cpp` with the following contents:
77
78.. code-block:: cpp
79
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020080 #include <pybind11/pybind11.h>
Wenzel Jakob93296692015-10-13 23:21:54 +020081
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020082 int add(int i, int j) {
83 return i + j;
84 }
85
Wenzel Jakob10e62e12015-10-15 22:46:07 +020086 namespace py = pybind11;
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020087
Wenzel Jakobb1b71402015-10-18 16:48:30 +020088 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020089 py::module m("example", "pybind11 example plugin");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020090
91 m.def("add", &add, "A function which adds two numbers");
92
93 return m.ptr();
94 }
95
Wenzel Jakobb1b71402015-10-18 16:48:30 +020096The :func:`PYBIND11_PLUGIN` macro creates a function that will be called when an
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020097``import`` statement is issued from within Python. The next line creates a
98module named ``example`` (with the supplied docstring). The method
99:func:`module::def` generates binding code that exposes the
100``add()`` function to Python. The last line returns the internal Python object
101associated with ``m`` to the Python interpreter.
102
103.. note::
104
105 Notice how little code was needed to expose our function to Python: all
106 details regarding the function's parameters and return value were
107 automatically inferred using template metaprogramming. This overall
108 approach and the used syntax are borrowed from Boost.Python, though the
109 underlying implementation is very different.
110
111pybind11 is a header-only-library, hence it is not necessary to link against
112any special libraries (other than Python itself). On Windows, use the CMake
113build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above
114example can be compiled using the following command
115
116.. code-block:: bash
117
Wenzel Jakob06f56ee2016-04-28 16:25:24 +0200118 $ c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200119
120In general, it is advisable to include several additional build parameters
121that can considerably reduce the size of the created binary. Refer to section
122:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
123build system.
124
125Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows)
126is located in the current directory, the following interactive Python session
127shows how to load and execute the example.
128
Wenzel Jakob99279f72016-06-03 11:19:29 +0200129.. code-block:: pycon
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200130
Wenzel Jakob93296692015-10-13 23:21:54 +0200131 $ python
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200132 Python 2.7.10 (default, Aug 22 2015, 20:33:39)
133 [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
134 Type "help", "copyright", "credits" or "license" for more information.
135 >>> import example
136 >>> example.add(1, 2)
137 3L
Wenzel Jakob93296692015-10-13 23:21:54 +0200138 >>>
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200139
140.. _keyword_args:
141
142Keyword arguments
143=================
144
145With a simple modification code, it is possible to inform Python about the
146names of the arguments ("i" and "j" in this case).
147
148.. code-block:: cpp
149
150 m.def("add", &add, "A function which adds two numbers",
151 py::arg("i"), py::arg("j"));
152
153:class:`arg` is one of several special tag classes which can be used to pass
154metadata into :func:`module::def`. With this modified binding code, we can now
155call the function using keyword arguments, which is a more readable alternative
156particularly for functions taking many parameters:
157
Wenzel Jakob99279f72016-06-03 11:19:29 +0200158.. code-block:: pycon
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200159
160 >>> import example
161 >>> example.add(i=1, j=2)
162 3L
163
164The keyword names also appear in the function signatures within the documentation.
165
Wenzel Jakob99279f72016-06-03 11:19:29 +0200166.. code-block:: pycon
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200167
168 >>> help(example)
169
170 ....
171
172 FUNCTIONS
173 add(...)
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100174 Signature : (i: int, j: int) -> int
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200175
176 A function which adds two numbers
177
Dean Moldovanb3eadfa2016-06-03 23:48:31 +0200178A shorter notation for named arguments is also available:
179
180.. code-block:: cpp
Wenzel Jakobfe342412016-09-06 13:02:29 +0900181
Dean Moldovanb3eadfa2016-06-03 23:48:31 +0200182 // regular notation
183 m.def("add1", &add, py::arg("i"), py::arg("j"));
184 // shorthand
185 using namespace pybind11::literals;
186 m.def("add2", &add, "i"_a, "j"_a);
187
Wenzel Jakobfe342412016-09-06 13:02:29 +0900188The :var:`_a` suffix forms a C++11 literal which is equivalent to :class:`arg`.
189Note that the literal operator must first be made visible with the directive
190``using namespace pybind11::literals``. This does not bring in anything else
Dean Moldovanb3eadfa2016-06-03 23:48:31 +0200191from the ``pybind11`` namespace except for literals.
192
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200193.. _default_args:
194
195Default arguments
196=================
197
198Suppose now that the function to be bound has default arguments, e.g.:
199
200.. code-block:: cpp
201
202 int add(int i = 1, int j = 2) {
203 return i + j;
204 }
205
206Unfortunately, pybind11 cannot automatically extract these parameters, since they
207are not part of the function's type information. However, they are simple to specify
208using an extension of :class:`arg`:
209
210.. code-block:: cpp
211
212 m.def("add", &add, "A function which adds two numbers",
213 py::arg("i") = 1, py::arg("j") = 2);
214
215The default values also appear within the documentation.
216
Wenzel Jakob99279f72016-06-03 11:19:29 +0200217.. code-block:: pycon
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200218
219 >>> help(example)
220
221 ....
222
223 FUNCTIONS
224 add(...)
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100225 Signature : (i: int = 1, j: int = 2) -> int
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200226
227 A function which adds two numbers
228
Dean Moldovanb3eadfa2016-06-03 23:48:31 +0200229The shorthand notation is also available for default arguments:
230
231.. code-block:: cpp
Wenzel Jakobfe342412016-09-06 13:02:29 +0900232
Dean Moldovanb3eadfa2016-06-03 23:48:31 +0200233 // regular notation
234 m.def("add1", &add, py::arg("i") = 1, py::arg("j") = 2);
235 // shorthand
236 m.def("add2", &add, "i"_a=1, "j"_a=2);
237
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200238.. _supported_types:
239
240Supported data types
241====================
242
243The following basic data types are supported out of the box (some may require
244an additional extension header to be included). To pass other data structures
Wenzel Jakob93296692015-10-13 23:21:54 +0200245as arguments and return values, refer to the section on binding :ref:`classes`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200246
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200247+---------------------------------+--------------------------+-------------------------------+
248| Data type | Description | Header file |
249+=================================+==========================+===============================+
250| ``int8_t``, ``uint8_t`` | 8-bit integers | :file:`pybind11/pybind11.h` |
251+---------------------------------+--------------------------+-------------------------------+
252| ``int16_t``, ``uint16_t`` | 16-bit integers | :file:`pybind11/pybind11.h` |
253+---------------------------------+--------------------------+-------------------------------+
254| ``int32_t``, ``uint32_t`` | 32-bit integers | :file:`pybind11/pybind11.h` |
255+---------------------------------+--------------------------+-------------------------------+
256| ``int64_t``, ``uint64_t`` | 64-bit integers | :file:`pybind11/pybind11.h` |
257+---------------------------------+--------------------------+-------------------------------+
258| ``ssize_t``, ``size_t`` | Platform-dependent size | :file:`pybind11/pybind11.h` |
259+---------------------------------+--------------------------+-------------------------------+
260| ``float``, ``double`` | Floating point types | :file:`pybind11/pybind11.h` |
261+---------------------------------+--------------------------+-------------------------------+
262| ``bool`` | Two-state Boolean type | :file:`pybind11/pybind11.h` |
263+---------------------------------+--------------------------+-------------------------------+
264| ``char`` | Character literal | :file:`pybind11/pybind11.h` |
265+---------------------------------+--------------------------+-------------------------------+
266| ``wchar_t`` | Wide character literal | :file:`pybind11/pybind11.h` |
267+---------------------------------+--------------------------+-------------------------------+
268| ``const char *`` | UTF-8 string literal | :file:`pybind11/pybind11.h` |
269+---------------------------------+--------------------------+-------------------------------+
270| ``const wchar_t *`` | Wide string literal | :file:`pybind11/pybind11.h` |
271+---------------------------------+--------------------------+-------------------------------+
272| ``std::string`` | STL dynamic UTF-8 string | :file:`pybind11/pybind11.h` |
273+---------------------------------+--------------------------+-------------------------------+
274| ``std::wstring`` | STL dynamic wide string | :file:`pybind11/pybind11.h` |
275+---------------------------------+--------------------------+-------------------------------+
276| ``std::pair<T1, T2>`` | Pair of two custom types | :file:`pybind11/pybind11.h` |
277+---------------------------------+--------------------------+-------------------------------+
278| ``std::tuple<...>`` | Arbitrary tuple of types | :file:`pybind11/pybind11.h` |
279+---------------------------------+--------------------------+-------------------------------+
280| ``std::reference_wrapper<...>`` | Reference type wrapper | :file:`pybind11/pybind11.h` |
281+---------------------------------+--------------------------+-------------------------------+
282| ``std::complex<T>`` | Complex numbers | :file:`pybind11/complex.h` |
283+---------------------------------+--------------------------+-------------------------------+
284| ``std::array<T, Size>`` | STL static array | :file:`pybind11/stl.h` |
285+---------------------------------+--------------------------+-------------------------------+
286| ``std::vector<T>`` | STL dynamic array | :file:`pybind11/stl.h` |
287+---------------------------------+--------------------------+-------------------------------+
288| ``std::list<T>`` | STL linked list | :file:`pybind11/stl.h` |
289+---------------------------------+--------------------------+-------------------------------+
290| ``std::map<T1, T2>`` | STL ordered map | :file:`pybind11/stl.h` |
291+---------------------------------+--------------------------+-------------------------------+
292| ``std::unordered_map<T1, T2>`` | STL unordered map | :file:`pybind11/stl.h` |
293+---------------------------------+--------------------------+-------------------------------+
294| ``std::set<T>`` | STL ordered set | :file:`pybind11/stl.h` |
295+---------------------------------+--------------------------+-------------------------------+
296| ``std::unordered_set<T>`` | STL unordered set | :file:`pybind11/stl.h` |
297+---------------------------------+--------------------------+-------------------------------+
298| ``std::function<...>`` | STL polymorphic function | :file:`pybind11/functional.h` |
299+---------------------------------+--------------------------+-------------------------------+
Trent Houliston352149e2016-08-25 23:08:04 +1000300| ``std::chrono::duration<...>`` | STL time duration | :file:`pybind11/chrono.h` |
301+---------------------------------+--------------------------+-------------------------------+
302| ``std::chrono::time_point<...>``| STL date/time | :file:`pybind11/chrono.h` |
303+---------------------------------+--------------------------+-------------------------------+
Wenzel Jakob6f017cf2016-09-06 14:13:35 +0900304| ``Eigen::Matrix<...>`` | Eigen: dense matrix | :file:`pybind11/eigen.h` |
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200305+---------------------------------+--------------------------+-------------------------------+
Wenzel Jakob6f017cf2016-09-06 14:13:35 +0900306| ``Eigen::Map<...>`` | Eigen: mapped memory | :file:`pybind11/eigen.h` |
307+---------------------------------+--------------------------+-------------------------------+
308| ``Eigen::SparseMatrix<...>`` | Eigen: sparse matrix | :file:`pybind11/eigen.h` |
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200309+---------------------------------+--------------------------+-------------------------------+
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200310
311
312.. [#f1] In practice, implementation and binding code will generally be located
313 in separate files.