Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1 | .. _basics: |
| 2 | |
| 3 | First steps |
| 4 | ########### |
| 5 | |
| 6 | This sections demonstrates the basic features of pybind11. Before getting |
| 7 | started, make sure that development environment is set up to compile the |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 8 | included set of test cases. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 9 | |
| 10 | |
| 11 | Compiling the test cases |
| 12 | ======================== |
| 13 | |
| 14 | Linux/MacOS |
| 15 | ----------- |
| 16 | |
| 17 | On Linux you'll need to install the **python-dev** or **python3-dev** packages as |
| 18 | well as **cmake**. On Mac OS, the included python version works out of the box, |
| 19 | but **cmake** must still be installed. |
| 20 | |
| 21 | After installing the prerequisites, run |
| 22 | |
| 23 | .. code-block:: bash |
| 24 | |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 25 | mkdir build |
| 26 | cd build |
| 27 | cmake .. |
Jason Rhinelander | 05920e3 | 2016-12-19 11:33:02 -0500 | [diff] [blame] | 28 | make check -j 4 |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 29 | |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 30 | The last line will both compile and run the tests. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 31 | |
| 32 | Windows |
| 33 | ------- |
| 34 | |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 35 | On Windows, only **Visual Studio 2015** and newer are supported since pybind11 relies |
| 36 | on various C++11 language features that break older versions of Visual Studio. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 37 | |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 38 | To compile and run the tests: |
| 39 | |
| 40 | .. code-block:: batch |
| 41 | |
| 42 | mkdir build |
| 43 | cd build |
| 44 | cmake .. |
Jason Rhinelander | 05920e3 | 2016-12-19 11:33:02 -0500 | [diff] [blame] | 45 | cmake --build . --config Release --target check |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 46 | |
| 47 | This will create a Visual Studio project, compile and run the target, all from the |
| 48 | command line. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 49 | |
| 50 | .. Note:: |
| 51 | |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 52 | 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 Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 56 | |
| 57 | .. seealso:: |
| 58 | |
| 59 | Advanced users who are already familiar with Boost.Python may want to skip |
Dean Moldovan | ec0d38e | 2016-08-13 03:09:52 +0200 | [diff] [blame] | 60 | the tutorial and look at the test cases in the :file:`tests` directory, |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 61 | which exercise all features of pybind11. |
| 62 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 63 | Header and namespace conventions |
| 64 | ================================ |
| 65 | |
| 66 | For brevity, all code examples assume that the following two lines are present: |
| 67 | |
| 68 | .. code-block:: cpp |
| 69 | |
| 70 | #include <pybind11/pybind11.h> |
| 71 | |
| 72 | namespace py = pybind11; |
| 73 | |
| 74 | Some features may require additional headers, but those will be specified as needed. |
| 75 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 76 | Creating bindings for a simple function |
| 77 | ======================================= |
| 78 | |
| 79 | Let's start by creating Python bindings for an extremely simple function, which |
| 80 | adds two numbers and returns their result: |
| 81 | |
| 82 | .. code-block:: cpp |
| 83 | |
| 84 | int add(int i, int j) { |
| 85 | return i + j; |
| 86 | } |
| 87 | |
| 88 | For simplicity [#f1]_, we'll put both this function and the binding code into |
| 89 | a file named :file:`example.cpp` with the following contents: |
| 90 | |
| 91 | .. code-block:: cpp |
| 92 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 93 | #include <pybind11/pybind11.h> |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 94 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 95 | int add(int i, int j) { |
| 96 | return i + j; |
| 97 | } |
| 98 | |
Wenzel Jakob | 10e62e1 | 2015-10-15 22:46:07 +0200 | [diff] [blame] | 99 | namespace py = pybind11; |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 100 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 101 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 102 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 103 | |
| 104 | m.def("add", &add, "A function which adds two numbers"); |
| 105 | |
| 106 | return m.ptr(); |
| 107 | } |
| 108 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 109 | .. [#f1] In practice, implementation and binding code will generally be located |
| 110 | in separate files. |
| 111 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 112 | The :func:`PYBIND11_PLUGIN` macro creates a function that will be called when an |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 113 | ``import`` statement is issued from within Python. The next line creates a |
| 114 | module named ``example`` (with the supplied docstring). The method |
| 115 | :func:`module::def` generates binding code that exposes the |
| 116 | ``add()`` function to Python. The last line returns the internal Python object |
| 117 | associated with ``m`` to the Python interpreter. |
| 118 | |
| 119 | .. note:: |
| 120 | |
| 121 | Notice how little code was needed to expose our function to Python: all |
| 122 | details regarding the function's parameters and return value were |
| 123 | automatically inferred using template metaprogramming. This overall |
| 124 | approach and the used syntax are borrowed from Boost.Python, though the |
| 125 | underlying implementation is very different. |
| 126 | |
| 127 | pybind11 is a header-only-library, hence it is not necessary to link against |
| 128 | any special libraries (other than Python itself). On Windows, use the CMake |
| 129 | build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above |
| 130 | example can be compiled using the following command |
| 131 | |
| 132 | .. code-block:: bash |
| 133 | |
Wenzel Jakob | 06f56ee | 2016-04-28 16:25:24 +0200 | [diff] [blame] | 134 | $ c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 135 | |
| 136 | In general, it is advisable to include several additional build parameters |
| 137 | that can considerably reduce the size of the created binary. Refer to section |
| 138 | :ref:`cmake` for a detailed example of a suitable cross-platform CMake-based |
| 139 | build system. |
| 140 | |
| 141 | Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows) |
| 142 | is located in the current directory, the following interactive Python session |
| 143 | shows how to load and execute the example. |
| 144 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 145 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 146 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 147 | $ python |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 148 | Python 2.7.10 (default, Aug 22 2015, 20:33:39) |
| 149 | [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin |
| 150 | Type "help", "copyright", "credits" or "license" for more information. |
| 151 | >>> import example |
| 152 | >>> example.add(1, 2) |
| 153 | 3L |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 154 | >>> |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 155 | |
| 156 | .. _keyword_args: |
| 157 | |
| 158 | Keyword arguments |
| 159 | ================= |
| 160 | |
| 161 | With a simple modification code, it is possible to inform Python about the |
| 162 | names of the arguments ("i" and "j" in this case). |
| 163 | |
| 164 | .. code-block:: cpp |
| 165 | |
| 166 | m.def("add", &add, "A function which adds two numbers", |
| 167 | py::arg("i"), py::arg("j")); |
| 168 | |
| 169 | :class:`arg` is one of several special tag classes which can be used to pass |
| 170 | metadata into :func:`module::def`. With this modified binding code, we can now |
| 171 | call the function using keyword arguments, which is a more readable alternative |
| 172 | particularly for functions taking many parameters: |
| 173 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 174 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 175 | |
| 176 | >>> import example |
| 177 | >>> example.add(i=1, j=2) |
| 178 | 3L |
| 179 | |
| 180 | The keyword names also appear in the function signatures within the documentation. |
| 181 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 182 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 183 | |
| 184 | >>> help(example) |
| 185 | |
| 186 | .... |
| 187 | |
| 188 | FUNCTIONS |
| 189 | add(...) |
Wenzel Jakob | 6eb11da | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 190 | Signature : (i: int, j: int) -> int |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 191 | |
| 192 | A function which adds two numbers |
| 193 | |
Dean Moldovan | b3eadfa | 2016-06-03 23:48:31 +0200 | [diff] [blame] | 194 | A shorter notation for named arguments is also available: |
| 195 | |
| 196 | .. code-block:: cpp |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 197 | |
Dean Moldovan | b3eadfa | 2016-06-03 23:48:31 +0200 | [diff] [blame] | 198 | // regular notation |
| 199 | m.def("add1", &add, py::arg("i"), py::arg("j")); |
| 200 | // shorthand |
| 201 | using namespace pybind11::literals; |
| 202 | m.def("add2", &add, "i"_a, "j"_a); |
| 203 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 204 | The :var:`_a` suffix forms a C++11 literal which is equivalent to :class:`arg`. |
| 205 | Note that the literal operator must first be made visible with the directive |
| 206 | ``using namespace pybind11::literals``. This does not bring in anything else |
Dean Moldovan | b3eadfa | 2016-06-03 23:48:31 +0200 | [diff] [blame] | 207 | from the ``pybind11`` namespace except for literals. |
| 208 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 209 | .. _default_args: |
| 210 | |
| 211 | Default arguments |
| 212 | ================= |
| 213 | |
| 214 | Suppose now that the function to be bound has default arguments, e.g.: |
| 215 | |
| 216 | .. code-block:: cpp |
| 217 | |
| 218 | int add(int i = 1, int j = 2) { |
| 219 | return i + j; |
| 220 | } |
| 221 | |
| 222 | Unfortunately, pybind11 cannot automatically extract these parameters, since they |
| 223 | are not part of the function's type information. However, they are simple to specify |
| 224 | using an extension of :class:`arg`: |
| 225 | |
| 226 | .. code-block:: cpp |
| 227 | |
| 228 | m.def("add", &add, "A function which adds two numbers", |
| 229 | py::arg("i") = 1, py::arg("j") = 2); |
| 230 | |
| 231 | The default values also appear within the documentation. |
| 232 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 233 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 234 | |
| 235 | >>> help(example) |
| 236 | |
| 237 | .... |
| 238 | |
| 239 | FUNCTIONS |
| 240 | add(...) |
Wenzel Jakob | 6eb11da | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 241 | Signature : (i: int = 1, j: int = 2) -> int |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 242 | |
| 243 | A function which adds two numbers |
| 244 | |
Dean Moldovan | b3eadfa | 2016-06-03 23:48:31 +0200 | [diff] [blame] | 245 | The shorthand notation is also available for default arguments: |
| 246 | |
| 247 | .. code-block:: cpp |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 248 | |
Dean Moldovan | b3eadfa | 2016-06-03 23:48:31 +0200 | [diff] [blame] | 249 | // regular notation |
| 250 | m.def("add1", &add, py::arg("i") = 1, py::arg("j") = 2); |
| 251 | // shorthand |
| 252 | m.def("add2", &add, "i"_a=1, "j"_a=2); |
| 253 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 254 | Exporting variables |
| 255 | =================== |
| 256 | |
Jason Rhinelander | 3f1ff3f | 2016-12-12 17:42:52 -0500 | [diff] [blame] | 257 | To expose a value from C++, use the ``attr`` function to register it in a |
| 258 | module as shown below. Built-in types and general objects (more on that later) |
| 259 | are automatically converted when assigned as attributes, and can be explicitly |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 260 | converted using the function ``py::cast``. |
| 261 | |
| 262 | .. code-block:: cpp |
| 263 | |
| 264 | PYBIND11_PLUGIN(example) { |
| 265 | py::module m("example", "pybind11 example plugin"); |
Jason Rhinelander | 3f1ff3f | 2016-12-12 17:42:52 -0500 | [diff] [blame] | 266 | m.attr("the_answer") = 42; |
| 267 | py::object world = py::cast("World"); |
| 268 | m.attr("what") = world; |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 269 | return m.ptr(); |
| 270 | } |
| 271 | |
| 272 | These are then accessible from Python: |
| 273 | |
| 274 | .. code-block:: pycon |
| 275 | |
| 276 | >>> import example |
| 277 | >>> example.the_answer |
| 278 | 42 |
| 279 | >>> example.what |
| 280 | 'World' |
| 281 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 282 | .. _supported_types: |
| 283 | |
| 284 | Supported data types |
| 285 | ==================== |
| 286 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 287 | A large number of data types are supported out of the box and can be used |
| 288 | seamlessly as functions arguments, return values or with ``py::cast`` in general. |
| 289 | For a full overview, see the :doc:`advanced/cast/index` section. |