Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1 | .. _classes: |
| 2 | |
| 3 | Object-oriented code |
| 4 | #################### |
| 5 | |
| 6 | Creating bindings for a custom type |
| 7 | =================================== |
| 8 | |
| 9 | Let's now look at a more complex example where we'll create bindings for a |
| 10 | custom C++ data structure named ``Pet``. Its definition is given below: |
| 11 | |
| 12 | .. code-block:: cpp |
| 13 | |
| 14 | struct Pet { |
| 15 | Pet(const std::string &name) : name(name) { } |
| 16 | void setName(const std::string &name_) { name = name_; } |
| 17 | const std::string &getName() const { return name; } |
| 18 | |
| 19 | std::string name; |
| 20 | }; |
| 21 | |
| 22 | The binding code for ``Pet`` looks as follows: |
| 23 | |
| 24 | .. code-block:: cpp |
| 25 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 26 | #include <pybind11/pybind11.h> |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 27 | |
Wenzel Jakob | 10e62e1 | 2015-10-15 22:46:07 +0200 | [diff] [blame] | 28 | namespace py = pybind11; |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 29 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 30 | PYBIND11_MODULE(example, m) { |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 31 | py::class_<Pet>(m, "Pet") |
| 32 | .def(py::init<const std::string &>()) |
| 33 | .def("setName", &Pet::setName) |
| 34 | .def("getName", &Pet::getName); |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 37 | :class:`class_` creates bindings for a C++ *class* or *struct*-style data |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 38 | structure. :func:`init` is a convenience function that takes the types of a |
| 39 | constructor's parameters as template arguments and wraps the corresponding |
| 40 | constructor (see the :ref:`custom_constructors` section for details). An |
| 41 | interactive Python session demonstrating this example is shown below: |
| 42 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 43 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 44 | |
| 45 | % python |
| 46 | >>> import example |
| 47 | >>> p = example.Pet('Molly') |
| 48 | >>> print(p) |
| 49 | <example.Pet object at 0x10cd98060> |
| 50 | >>> p.getName() |
| 51 | u'Molly' |
| 52 | >>> p.setName('Charly') |
| 53 | >>> p.getName() |
| 54 | u'Charly' |
| 55 | |
Wenzel Jakob | 43b6a23 | 2016-02-07 17:24:41 +0100 | [diff] [blame] | 56 | .. seealso:: |
| 57 | |
| 58 | Static member functions can be bound in the same way using |
| 59 | :func:`class_::def_static`. |
| 60 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 61 | Keyword and default arguments |
| 62 | ============================= |
| 63 | It is possible to specify keyword and default arguments using the syntax |
| 64 | discussed in the previous chapter. Refer to the sections :ref:`keyword_args` |
| 65 | and :ref:`default_args` for details. |
| 66 | |
| 67 | Binding lambda functions |
| 68 | ======================== |
| 69 | |
| 70 | Note how ``print(p)`` produced a rather useless summary of our data structure in the example above: |
| 71 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 72 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 73 | |
| 74 | >>> print(p) |
| 75 | <example.Pet object at 0x10cd98060> |
| 76 | |
| 77 | To address this, we could bind an utility function that returns a human-readable |
| 78 | summary to the special method slot named ``__repr__``. Unfortunately, there is no |
| 79 | suitable functionality in the ``Pet`` data structure, and it would be nice if |
| 80 | we did not have to change it. This can easily be accomplished by binding a |
| 81 | Lambda function instead: |
| 82 | |
| 83 | .. code-block:: cpp |
| 84 | |
| 85 | py::class_<Pet>(m, "Pet") |
| 86 | .def(py::init<const std::string &>()) |
| 87 | .def("setName", &Pet::setName) |
| 88 | .def("getName", &Pet::getName) |
| 89 | .def("__repr__", |
| 90 | [](const Pet &a) { |
| 91 | return "<example.Pet named '" + a.name + "'>"; |
| 92 | } |
| 93 | ); |
| 94 | |
| 95 | Both stateless [#f1]_ and stateful lambda closures are supported by pybind11. |
| 96 | With the above change, the same Python code now produces the following output: |
| 97 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 98 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 99 | |
| 100 | >>> print(p) |
| 101 | <example.Pet named 'Molly'> |
| 102 | |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 103 | .. [#f1] Stateless closures are those with an empty pair of brackets ``[]`` as the capture object. |
| 104 | |
Wenzel Jakob | f88af0c | 2016-06-22 13:52:31 +0200 | [diff] [blame] | 105 | .. _properties: |
| 106 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 107 | Instance and static fields |
| 108 | ========================== |
| 109 | |
| 110 | We can also directly expose the ``name`` field using the |
| 111 | :func:`class_::def_readwrite` method. A similar :func:`class_::def_readonly` |
| 112 | method also exists for ``const`` fields. |
| 113 | |
| 114 | .. code-block:: cpp |
| 115 | |
| 116 | py::class_<Pet>(m, "Pet") |
| 117 | .def(py::init<const std::string &>()) |
| 118 | .def_readwrite("name", &Pet::name) |
| 119 | // ... remainder ... |
| 120 | |
| 121 | This makes it possible to write |
| 122 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 123 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 124 | |
| 125 | >>> p = example.Pet('Molly') |
| 126 | >>> p.name |
| 127 | u'Molly' |
| 128 | >>> p.name = 'Charly' |
| 129 | >>> p.name |
| 130 | u'Charly' |
| 131 | |
| 132 | Now suppose that ``Pet::name`` was a private internal variable |
| 133 | that can only be accessed via setters and getters. |
| 134 | |
| 135 | .. code-block:: cpp |
| 136 | |
| 137 | class Pet { |
| 138 | public: |
| 139 | Pet(const std::string &name) : name(name) { } |
| 140 | void setName(const std::string &name_) { name = name_; } |
| 141 | const std::string &getName() const { return name; } |
| 142 | private: |
| 143 | std::string name; |
| 144 | }; |
| 145 | |
| 146 | In this case, the method :func:`class_::def_property` |
| 147 | (:func:`class_::def_property_readonly` for read-only data) can be used to |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 148 | provide a field-like interface within Python that will transparently call |
| 149 | the setter and getter functions: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 150 | |
| 151 | .. code-block:: cpp |
| 152 | |
| 153 | py::class_<Pet>(m, "Pet") |
| 154 | .def(py::init<const std::string &>()) |
| 155 | .def_property("name", &Pet::getName, &Pet::setName) |
| 156 | // ... remainder ... |
| 157 | |
| 158 | .. seealso:: |
| 159 | |
| 160 | Similar functions :func:`class_::def_readwrite_static`, |
| 161 | :func:`class_::def_readonly_static` :func:`class_::def_property_static`, |
| 162 | and :func:`class_::def_property_readonly_static` are provided for binding |
Wenzel Jakob | f88af0c | 2016-06-22 13:52:31 +0200 | [diff] [blame] | 163 | static variables and properties. Please also see the section on |
| 164 | :ref:`static_properties` in the advanced part of the documentation. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 165 | |
Dean Moldovan | 9273af4 | 2016-10-13 23:53:16 +0200 | [diff] [blame] | 166 | Dynamic attributes |
| 167 | ================== |
| 168 | |
| 169 | Native Python classes can pick up new attributes dynamically: |
| 170 | |
| 171 | .. code-block:: pycon |
| 172 | |
| 173 | >>> class Pet: |
| 174 | ... name = 'Molly' |
| 175 | ... |
| 176 | >>> p = Pet() |
| 177 | >>> p.name = 'Charly' # overwrite existing |
| 178 | >>> p.age = 2 # dynamically add a new attribute |
| 179 | |
| 180 | By default, classes exported from C++ do not support this and the only writable |
| 181 | attributes are the ones explicitly defined using :func:`class_::def_readwrite` |
| 182 | or :func:`class_::def_property`. |
| 183 | |
| 184 | .. code-block:: cpp |
| 185 | |
| 186 | py::class_<Pet>(m, "Pet") |
| 187 | .def(py::init<>()) |
| 188 | .def_readwrite("name", &Pet::name); |
| 189 | |
| 190 | Trying to set any other attribute results in an error: |
| 191 | |
| 192 | .. code-block:: pycon |
| 193 | |
| 194 | >>> p = example.Pet() |
| 195 | >>> p.name = 'Charly' # OK, attribute defined in C++ |
| 196 | >>> p.age = 2 # fail |
| 197 | AttributeError: 'Pet' object has no attribute 'age' |
| 198 | |
| 199 | To enable dynamic attributes for C++ classes, the :class:`py::dynamic_attr` tag |
| 200 | must be added to the :class:`py::class_` constructor: |
| 201 | |
| 202 | .. code-block:: cpp |
| 203 | |
| 204 | py::class_<Pet>(m, "Pet", py::dynamic_attr()) |
| 205 | .def(py::init<>()) |
| 206 | .def_readwrite("name", &Pet::name); |
| 207 | |
| 208 | Now everything works as expected: |
| 209 | |
| 210 | .. code-block:: pycon |
| 211 | |
| 212 | >>> p = example.Pet() |
| 213 | >>> p.name = 'Charly' # OK, overwrite value in C++ |
| 214 | >>> p.age = 2 # OK, dynamically add a new attribute |
| 215 | >>> p.__dict__ # just like a native Python class |
| 216 | {'age': 2} |
| 217 | |
| 218 | Note that there is a small runtime cost for a class with dynamic attributes. |
| 219 | Not only because of the addition of a ``__dict__``, but also because of more |
| 220 | expensive garbage collection tracking which must be activated to resolve |
| 221 | possible circular references. Native Python classes incur this same cost by |
| 222 | default, so this is not anything to worry about. By default, pybind11 classes |
| 223 | are more efficient than native Python classes. Enabling dynamic attributes |
| 224 | just brings them on par. |
| 225 | |
Wenzel Jakob | 2dfbade | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 226 | .. _inheritance: |
| 227 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 228 | Inheritance |
| 229 | =========== |
| 230 | |
| 231 | Suppose now that the example consists of two data structures with an |
| 232 | inheritance relationship: |
| 233 | |
| 234 | .. code-block:: cpp |
| 235 | |
| 236 | struct Pet { |
| 237 | Pet(const std::string &name) : name(name) { } |
| 238 | std::string name; |
| 239 | }; |
| 240 | |
| 241 | struct Dog : Pet { |
| 242 | Dog(const std::string &name) : Pet(name) { } |
| 243 | std::string bark() const { return "woof!"; } |
| 244 | }; |
| 245 | |
Wenzel Jakob | bad589a | 2016-09-12 12:03:20 +0900 | [diff] [blame] | 246 | There are two different ways of indicating a hierarchical relationship to |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 247 | pybind11: the first specifies the C++ base class as an extra template |
Wenzel Jakob | bad589a | 2016-09-12 12:03:20 +0900 | [diff] [blame] | 248 | parameter of the :class:`class_`: |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 249 | |
| 250 | .. code-block:: cpp |
| 251 | |
| 252 | py::class_<Pet>(m, "Pet") |
| 253 | .def(py::init<const std::string &>()) |
| 254 | .def_readwrite("name", &Pet::name); |
| 255 | |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 256 | // Method 1: template parameter: |
| 257 | py::class_<Dog, Pet /* <- specify C++ parent type */>(m, "Dog") |
| 258 | .def(py::init<const std::string &>()) |
| 259 | .def("bark", &Dog::bark); |
| 260 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 261 | Alternatively, we can also assign a name to the previously bound ``Pet`` |
| 262 | :class:`class_` object and reference it when binding the ``Dog`` class: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 263 | |
| 264 | .. code-block:: cpp |
| 265 | |
| 266 | py::class_<Pet> pet(m, "Pet"); |
| 267 | pet.def(py::init<const std::string &>()) |
| 268 | .def_readwrite("name", &Pet::name); |
| 269 | |
Wenzel Jakob | bad589a | 2016-09-12 12:03:20 +0900 | [diff] [blame] | 270 | // Method 2: pass parent class_ object: |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 271 | py::class_<Dog>(m, "Dog", pet /* <- specify Python parent type */) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 272 | .def(py::init<const std::string &>()) |
| 273 | .def("bark", &Dog::bark); |
| 274 | |
Wenzel Jakob | bad589a | 2016-09-12 12:03:20 +0900 | [diff] [blame] | 275 | Functionality-wise, both approaches are equivalent. Afterwards, instances will |
| 276 | expose fields and methods of both types: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 277 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 278 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 279 | |
| 280 | >>> p = example.Dog('Molly') |
| 281 | >>> p.name |
| 282 | u'Molly' |
| 283 | >>> p.bark() |
| 284 | u'woof!' |
| 285 | |
| 286 | Overloaded methods |
| 287 | ================== |
| 288 | |
| 289 | Sometimes there are several overloaded C++ methods with the same name taking |
| 290 | different kinds of input arguments: |
| 291 | |
| 292 | .. code-block:: cpp |
| 293 | |
| 294 | struct Pet { |
| 295 | Pet(const std::string &name, int age) : name(name), age(age) { } |
| 296 | |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 297 | void set(int age_) { age = age_; } |
| 298 | void set(const std::string &name_) { name = name_; } |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 299 | |
| 300 | std::string name; |
| 301 | int age; |
| 302 | }; |
| 303 | |
| 304 | Attempting to bind ``Pet::set`` will cause an error since the compiler does not |
| 305 | know which method the user intended to select. We can disambiguate by casting |
| 306 | them to function pointers. Binding multiple functions to the same Python name |
Wenzel Jakob | 0fb8528 | 2015-10-19 23:50:51 +0200 | [diff] [blame] | 307 | automatically creates a chain of function overloads that will be tried in |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 308 | sequence. |
| 309 | |
| 310 | .. code-block:: cpp |
| 311 | |
| 312 | py::class_<Pet>(m, "Pet") |
| 313 | .def(py::init<const std::string &, int>()) |
| 314 | .def("set", (void (Pet::*)(int)) &Pet::set, "Set the pet's age") |
| 315 | .def("set", (void (Pet::*)(const std::string &)) &Pet::set, "Set the pet's name"); |
| 316 | |
| 317 | The overload signatures are also visible in the method's docstring: |
| 318 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 319 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 320 | |
| 321 | >>> help(example.Pet) |
| 322 | |
| 323 | class Pet(__builtin__.object) |
| 324 | | Methods defined here: |
| 325 | | |
| 326 | | __init__(...) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 327 | | Signature : (Pet, str, int) -> NoneType |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 328 | | |
| 329 | | set(...) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 330 | | 1. Signature : (Pet, int) -> NoneType |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 331 | | |
| 332 | | Set the pet's age |
| 333 | | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 334 | | 2. Signature : (Pet, str) -> NoneType |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 335 | | |
| 336 | | Set the pet's name |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 337 | |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 338 | If you have a C++14 compatible compiler [#cpp14]_, you can use an alternative |
| 339 | syntax to cast the overloaded function: |
| 340 | |
| 341 | .. code-block:: cpp |
| 342 | |
| 343 | py::class_<Pet>(m, "Pet") |
| 344 | .def("set", py::overload_cast<int>(&Pet::set), "Set the pet's age") |
| 345 | .def("set", py::overload_cast<const std::string &>(&Pet::set), "Set the pet's name"); |
| 346 | |
| 347 | Here, ``py::overload_cast`` only requires the parameter types to be specified. |
| 348 | The return type and class are deduced. This avoids the additional noise of |
| 349 | ``void (Pet::*)()`` as seen in the raw cast. If a function is overloaded based |
| 350 | on constness, the ``py::const_`` tag should be used: |
| 351 | |
| 352 | .. code-block:: cpp |
| 353 | |
| 354 | struct Widget { |
| 355 | int foo(int x, float y); |
| 356 | int foo(int x, float y) const; |
| 357 | }; |
| 358 | |
| 359 | py::class_<Widget>(m, "Widget") |
| 360 | .def("foo_mutable", py::overload_cast<int, float>(&Widget::foo)) |
| 361 | .def("foo_const", py::overload_cast<int, float>(&Widget::foo, py::const_)); |
| 362 | |
| 363 | |
| 364 | .. [#cpp14] A compiler which supports the ``-std=c++14`` flag |
| 365 | or Visual Studio 2015 Update 2 and newer. |
| 366 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 367 | .. note:: |
| 368 | |
| 369 | To define multiple overloaded constructors, simply declare one after the |
| 370 | other using the ``.def(py::init<...>())`` syntax. The existing machinery |
| 371 | for specifying keyword and default arguments also works. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 372 | |
| 373 | Enumerations and internal types |
| 374 | =============================== |
| 375 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 376 | Let's now suppose that the example class contains an internal enumeration type, |
| 377 | e.g.: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 378 | |
| 379 | .. code-block:: cpp |
| 380 | |
| 381 | struct Pet { |
| 382 | enum Kind { |
| 383 | Dog = 0, |
| 384 | Cat |
| 385 | }; |
| 386 | |
| 387 | Pet(const std::string &name, Kind type) : name(name), type(type) { } |
| 388 | |
| 389 | std::string name; |
| 390 | Kind type; |
| 391 | }; |
| 392 | |
| 393 | The binding code for this example looks as follows: |
| 394 | |
| 395 | .. code-block:: cpp |
| 396 | |
| 397 | py::class_<Pet> pet(m, "Pet"); |
| 398 | |
| 399 | pet.def(py::init<const std::string &, Pet::Kind>()) |
| 400 | .def_readwrite("name", &Pet::name) |
| 401 | .def_readwrite("type", &Pet::type); |
| 402 | |
| 403 | py::enum_<Pet::Kind>(pet, "Kind") |
| 404 | .value("Dog", Pet::Kind::Dog) |
| 405 | .value("Cat", Pet::Kind::Cat) |
| 406 | .export_values(); |
| 407 | |
| 408 | To ensure that the ``Kind`` type is created within the scope of ``Pet``, the |
| 409 | ``pet`` :class:`class_` instance must be supplied to the :class:`enum_`. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 410 | constructor. The :func:`enum_::export_values` function exports the enum entries |
| 411 | into the parent scope, which should be skipped for newer C++11-style strongly |
| 412 | typed enums. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 413 | |
Wenzel Jakob | 99279f7 | 2016-06-03 11:19:29 +0200 | [diff] [blame] | 414 | .. code-block:: pycon |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 415 | |
| 416 | >>> p = Pet('Lucy', Pet.Cat) |
| 417 | >>> p.type |
| 418 | Kind.Cat |
| 419 | >>> int(p.type) |
| 420 | 1L |
| 421 | |
Matthieu Bec | af936e1 | 2017-03-03 08:45:50 -0800 | [diff] [blame] | 422 | The entries defined by the enumeration type are exposed in the ``__members__`` property: |
| 423 | |
| 424 | .. code-block:: pycon |
| 425 | |
| 426 | >>> Pet.Kind.__members__ |
| 427 | {'Dog': Kind.Dog, 'Cat': Kind.Cat} |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 428 | |
Wenzel Jakob | 405f6d1 | 2016-11-17 23:24:47 +0100 | [diff] [blame] | 429 | .. note:: |
| 430 | |
| 431 | When the special tag ``py::arithmetic()`` is specified to the ``enum_`` |
| 432 | constructor, pybind11 creates an enumeration that also supports rudimentary |
| 433 | arithmetic and bit-level operations like comparisons, and, or, xor, negation, |
| 434 | etc. |
| 435 | |
| 436 | .. code-block:: cpp |
| 437 | |
| 438 | py::enum_<Pet::Kind>(pet, "Kind", py::arithmetic()) |
| 439 | ... |
| 440 | |
| 441 | By default, these are omitted to conserve space. |