Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 1 | Classes |
| 2 | ####### |
| 3 | |
| 4 | This section presents advanced binding code for classes and it is assumed |
| 5 | that you are already familiar with the basics from :doc:`/classes`. |
| 6 | |
| 7 | .. _overriding_virtuals: |
| 8 | |
| 9 | Overriding virtual functions in Python |
| 10 | ====================================== |
| 11 | |
| 12 | Suppose that a C++ class or interface has a virtual function that we'd like to |
| 13 | to override from within Python (we'll focus on the class ``Animal``; ``Dog`` is |
| 14 | given as a specific example of how one would do this with traditional C++ |
| 15 | code). |
| 16 | |
| 17 | .. code-block:: cpp |
| 18 | |
| 19 | class Animal { |
| 20 | public: |
| 21 | virtual ~Animal() { } |
| 22 | virtual std::string go(int n_times) = 0; |
| 23 | }; |
| 24 | |
| 25 | class Dog : public Animal { |
| 26 | public: |
| 27 | std::string go(int n_times) override { |
| 28 | std::string result; |
| 29 | for (int i=0; i<n_times; ++i) |
| 30 | result += "woof! "; |
| 31 | return result; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | Let's also suppose that we are given a plain function which calls the |
| 36 | function ``go()`` on an arbitrary ``Animal`` instance. |
| 37 | |
| 38 | .. code-block:: cpp |
| 39 | |
| 40 | std::string call_go(Animal *animal) { |
| 41 | return animal->go(3); |
| 42 | } |
| 43 | |
| 44 | Normally, the binding code for these classes would look as follows: |
| 45 | |
| 46 | .. code-block:: cpp |
| 47 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 48 | PYBIND11_MODULE(example, m) { |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 49 | py::class_<Animal> animal(m, "Animal"); |
| 50 | animal |
| 51 | .def("go", &Animal::go); |
| 52 | |
| 53 | py::class_<Dog>(m, "Dog", animal) |
| 54 | .def(py::init<>()); |
| 55 | |
| 56 | m.def("call_go", &call_go); |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | However, these bindings are impossible to extend: ``Animal`` is not |
| 60 | constructible, and we clearly require some kind of "trampoline" that |
| 61 | redirects virtual calls back to Python. |
| 62 | |
| 63 | Defining a new type of ``Animal`` from within Python is possible but requires a |
| 64 | helper class that is defined as follows: |
| 65 | |
| 66 | .. code-block:: cpp |
| 67 | |
| 68 | class PyAnimal : public Animal { |
| 69 | public: |
| 70 | /* Inherit the constructors */ |
| 71 | using Animal::Animal; |
| 72 | |
| 73 | /* Trampoline (need one for each virtual function) */ |
| 74 | std::string go(int n_times) override { |
| 75 | PYBIND11_OVERLOAD_PURE( |
| 76 | std::string, /* Return type */ |
| 77 | Animal, /* Parent class */ |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 78 | go, /* Name of function in C++ (must match Python name) */ |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 79 | n_times /* Argument(s) */ |
| 80 | ); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | The macro :func:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual |
| 85 | functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have |
| 86 | a default implementation. There are also two alternate macros |
| 87 | :func:`PYBIND11_OVERLOAD_PURE_NAME` and :func:`PYBIND11_OVERLOAD_NAME` which |
| 88 | take a string-valued name argument between the *Parent class* and *Name of the |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 89 | function* slots, which defines the name of function in Python. This is required |
| 90 | when the C++ and Python versions of the |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 91 | function have different names, e.g. ``operator()`` vs ``__call__``. |
| 92 | |
| 93 | The binding code also needs a few minor adaptations (highlighted): |
| 94 | |
| 95 | .. code-block:: cpp |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 96 | :emphasize-lines: 2,4,5 |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 97 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 98 | PYBIND11_MODULE(example, m) { |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 99 | py::class_<Animal, PyAnimal /* <--- trampoline*/> animal(m, "Animal"); |
| 100 | animal |
| 101 | .def(py::init<>()) |
| 102 | .def("go", &Animal::go); |
| 103 | |
| 104 | py::class_<Dog>(m, "Dog", animal) |
| 105 | .def(py::init<>()); |
| 106 | |
| 107 | m.def("call_go", &call_go); |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Importantly, pybind11 is made aware of the trampoline helper class by |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 111 | specifying it as an extra template argument to :class:`class_`. (This can also |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 112 | be combined with other template arguments such as a custom holder type; the |
| 113 | order of template types does not matter). Following this, we are able to |
| 114 | define a constructor as usual. |
| 115 | |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 116 | Bindings should be made against the actual class, not the trampoline helper class. |
| 117 | |
| 118 | .. code-block:: cpp |
| 119 | |
| 120 | py::class_<Animal, PyAnimal /* <--- trampoline*/> animal(m, "Animal"); |
| 121 | animal |
| 122 | .def(py::init<>()) |
| 123 | .def("go", &PyAnimal::go); /* <--- THIS IS WRONG, use &Animal::go */ |
| 124 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 125 | Note, however, that the above is sufficient for allowing python classes to |
EricCousineau-TRI | e06077b | 2017-08-07 18:37:42 -0400 | [diff] [blame] | 126 | extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 127 | necessary steps required to providing proper overload support for inherited |
| 128 | classes. |
| 129 | |
| 130 | The Python session below shows how to override ``Animal::go`` and invoke it via |
| 131 | a virtual method call. |
| 132 | |
| 133 | .. code-block:: pycon |
| 134 | |
| 135 | >>> from example import * |
| 136 | >>> d = Dog() |
| 137 | >>> call_go(d) |
| 138 | u'woof! woof! woof! ' |
| 139 | >>> class Cat(Animal): |
| 140 | ... def go(self, n_times): |
| 141 | ... return "meow! " * n_times |
| 142 | ... |
| 143 | >>> c = Cat() |
| 144 | >>> call_go(c) |
| 145 | u'meow! meow! meow! ' |
| 146 | |
EricCousineau-TRI | e06077b | 2017-08-07 18:37:42 -0400 | [diff] [blame] | 147 | If you are defining a custom constructor in a derived Python class, you *must* |
| 148 | ensure that you explicitly call the bound C++ constructor using ``__init__``, |
| 149 | *regardless* of whether it is a default constructor or not. Otherwise, the |
| 150 | memory for the C++ portion of the instance will be left uninitialized, which |
| 151 | will generally leave the C++ instance in an invalid state and cause undefined |
| 152 | behavior if the C++ instance is subsequently used. |
| 153 | |
| 154 | Here is an example: |
| 155 | |
| 156 | .. code-block:: python |
| 157 | |
| 158 | class Dachschund(Dog): |
| 159 | def __init__(self, name): |
| 160 | Dog.__init__(self) # Without this, undefind behavior may occur if the C++ portions are referenced. |
| 161 | self.name = name |
| 162 | def bark(self): |
| 163 | return "yap!" |
| 164 | |
| 165 | Note that a direct ``__init__`` constructor *should be called*, and ``super()`` |
| 166 | should not be used. For simple cases of linear inheritance, ``super()`` |
| 167 | may work, but once you begin mixing Python and C++ multiple inheritance, |
| 168 | things will fall apart due to differences between Python's MRO and C++'s |
| 169 | mechanisms. |
| 170 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 171 | Please take a look at the :ref:`macro_notes` before using this feature. |
| 172 | |
| 173 | .. note:: |
| 174 | |
| 175 | When the overridden type returns a reference or pointer to a type that |
| 176 | pybind11 converts from Python (for example, numeric values, std::string, |
| 177 | and other built-in value-converting types), there are some limitations to |
| 178 | be aware of: |
| 179 | |
| 180 | - because in these cases there is no C++ variable to reference (the value |
| 181 | is stored in the referenced Python variable), pybind11 provides one in |
| 182 | the PYBIND11_OVERLOAD macros (when needed) with static storage duration. |
| 183 | Note that this means that invoking the overloaded method on *any* |
| 184 | instance will change the referenced value stored in *all* instances of |
| 185 | that type. |
| 186 | |
| 187 | - Attempts to modify a non-const reference will not have the desired |
| 188 | effect: it will change only the static cache variable, but this change |
| 189 | will not propagate to underlying Python instance, and the change will be |
| 190 | replaced the next time the overload is invoked. |
| 191 | |
| 192 | .. seealso:: |
| 193 | |
| 194 | The file :file:`tests/test_virtual_functions.cpp` contains a complete |
| 195 | example that demonstrates how to override virtual functions using pybind11 |
| 196 | in more detail. |
| 197 | |
| 198 | .. _virtual_and_inheritance: |
| 199 | |
| 200 | Combining virtual functions and inheritance |
| 201 | =========================================== |
| 202 | |
| 203 | When combining virtual methods with inheritance, you need to be sure to provide |
| 204 | an override for each method for which you want to allow overrides from derived |
| 205 | python classes. For example, suppose we extend the above ``Animal``/``Dog`` |
| 206 | example as follows: |
| 207 | |
| 208 | .. code-block:: cpp |
| 209 | |
| 210 | class Animal { |
| 211 | public: |
| 212 | virtual std::string go(int n_times) = 0; |
| 213 | virtual std::string name() { return "unknown"; } |
| 214 | }; |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 215 | class Dog : public Animal { |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 216 | public: |
| 217 | std::string go(int n_times) override { |
| 218 | std::string result; |
| 219 | for (int i=0; i<n_times; ++i) |
| 220 | result += bark() + " "; |
| 221 | return result; |
| 222 | } |
| 223 | virtual std::string bark() { return "woof!"; } |
| 224 | }; |
| 225 | |
| 226 | then the trampoline class for ``Animal`` must, as described in the previous |
| 227 | section, override ``go()`` and ``name()``, but in order to allow python code to |
| 228 | inherit properly from ``Dog``, we also need a trampoline class for ``Dog`` that |
| 229 | overrides both the added ``bark()`` method *and* the ``go()`` and ``name()`` |
| 230 | methods inherited from ``Animal`` (even though ``Dog`` doesn't directly |
| 231 | override the ``name()`` method): |
| 232 | |
| 233 | .. code-block:: cpp |
| 234 | |
| 235 | class PyAnimal : public Animal { |
| 236 | public: |
| 237 | using Animal::Animal; // Inherit constructors |
| 238 | std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Animal, go, n_times); } |
| 239 | std::string name() override { PYBIND11_OVERLOAD(std::string, Animal, name, ); } |
| 240 | }; |
| 241 | class PyDog : public Dog { |
| 242 | public: |
| 243 | using Dog::Dog; // Inherit constructors |
| 244 | std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Dog, go, n_times); } |
| 245 | std::string name() override { PYBIND11_OVERLOAD(std::string, Dog, name, ); } |
| 246 | std::string bark() override { PYBIND11_OVERLOAD(std::string, Dog, bark, ); } |
| 247 | }; |
| 248 | |
Wenzel Jakob | ab26259 | 2017-03-22 21:39:19 +0100 | [diff] [blame] | 249 | .. note:: |
| 250 | |
| 251 | Note the trailing commas in the ``PYBIND11_OVERLOAD`` calls to ``name()`` |
| 252 | and ``bark()``. These are needed to portably implement a trampoline for a |
| 253 | function that does not take any arguments. For functions that take |
| 254 | a nonzero number of arguments, the trailing comma must be omitted. |
| 255 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 256 | A registered class derived from a pybind11-registered class with virtual |
| 257 | methods requires a similar trampoline class, *even if* it doesn't explicitly |
| 258 | declare or override any virtual methods itself: |
| 259 | |
| 260 | .. code-block:: cpp |
| 261 | |
| 262 | class Husky : public Dog {}; |
| 263 | class PyHusky : public Husky { |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 264 | public: |
| 265 | using Husky::Husky; // Inherit constructors |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 266 | std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); } |
| 267 | std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); } |
| 268 | std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); } |
| 269 | }; |
| 270 | |
| 271 | There is, however, a technique that can be used to avoid this duplication |
| 272 | (which can be especially helpful for a base class with several virtual |
| 273 | methods). The technique involves using template trampoline classes, as |
| 274 | follows: |
| 275 | |
| 276 | .. code-block:: cpp |
| 277 | |
| 278 | template <class AnimalBase = Animal> class PyAnimal : public AnimalBase { |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 279 | public: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 280 | using AnimalBase::AnimalBase; // Inherit constructors |
| 281 | std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); } |
| 282 | std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); } |
| 283 | }; |
| 284 | template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> { |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 285 | public: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 286 | using PyAnimal<DogBase>::PyAnimal; // Inherit constructors |
| 287 | // Override PyAnimal's pure virtual go() with a non-pure one: |
| 288 | std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); } |
| 289 | std::string bark() override { PYBIND11_OVERLOAD(std::string, DogBase, bark, ); } |
| 290 | }; |
| 291 | |
| 292 | This technique has the advantage of requiring just one trampoline method to be |
| 293 | declared per virtual method and pure virtual method override. It does, |
| 294 | however, require the compiler to generate at least as many methods (and |
| 295 | possibly more, if both pure virtual and overridden pure virtual methods are |
| 296 | exposed, as above). |
| 297 | |
| 298 | The classes are then registered with pybind11 using: |
| 299 | |
| 300 | .. code-block:: cpp |
| 301 | |
| 302 | py::class_<Animal, PyAnimal<>> animal(m, "Animal"); |
| 303 | py::class_<Dog, PyDog<>> dog(m, "Dog"); |
| 304 | py::class_<Husky, PyDog<Husky>> husky(m, "Husky"); |
| 305 | // ... add animal, dog, husky definitions |
| 306 | |
| 307 | Note that ``Husky`` did not require a dedicated trampoline template class at |
| 308 | all, since it neither declares any new virtual methods nor provides any pure |
| 309 | virtual method implementations. |
| 310 | |
| 311 | With either the repeated-virtuals or templated trampoline methods in place, you |
| 312 | can now create a python class that inherits from ``Dog``: |
| 313 | |
| 314 | .. code-block:: python |
| 315 | |
| 316 | class ShihTzu(Dog): |
| 317 | def bark(self): |
| 318 | return "yip!" |
| 319 | |
| 320 | .. seealso:: |
| 321 | |
| 322 | See the file :file:`tests/test_virtual_functions.cpp` for complete examples |
| 323 | using both the duplication and templated trampoline approaches. |
| 324 | |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame^] | 325 | .. _extended_aliases: |
| 326 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 327 | Extended trampoline class functionality |
| 328 | ======================================= |
| 329 | |
| 330 | The trampoline classes described in the previous sections are, by default, only |
| 331 | initialized when needed. More specifically, they are initialized when a python |
| 332 | class actually inherits from a registered type (instead of merely creating an |
| 333 | instance of the registered type), or when a registered constructor is only |
| 334 | valid for the trampoline class but not the registered class. This is primarily |
| 335 | for performance reasons: when the trampoline class is not needed for anything |
| 336 | except virtual method dispatching, not initializing the trampoline class |
| 337 | improves performance by avoiding needing to do a run-time check to see if the |
| 338 | inheriting python instance has an overloaded method. |
| 339 | |
| 340 | Sometimes, however, it is useful to always initialize a trampoline class as an |
| 341 | intermediate class that does more than just handle virtual method dispatching. |
| 342 | For example, such a class might perform extra class initialization, extra |
| 343 | destruction operations, and might define new members and methods to enable a |
| 344 | more python-like interface to a class. |
| 345 | |
| 346 | In order to tell pybind11 that it should *always* initialize the trampoline |
| 347 | class when creating new instances of a type, the class constructors should be |
| 348 | declared using ``py::init_alias<Args, ...>()`` instead of the usual |
| 349 | ``py::init<Args, ...>()``. This forces construction via the trampoline class, |
| 350 | ensuring member initialization and (eventual) destruction. |
| 351 | |
| 352 | .. seealso:: |
| 353 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 354 | See the file :file:`tests/test_virtual_functions.cpp` for complete examples |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 355 | showing both normal and forced trampoline instantiation. |
| 356 | |
| 357 | .. _custom_constructors: |
| 358 | |
| 359 | Custom constructors |
| 360 | =================== |
| 361 | |
| 362 | The syntax for binding constructors was previously introduced, but it only |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame^] | 363 | works when a constructor of the appropriate arguments actually exists on the |
| 364 | C++ side. To extend this to more general cases, pybind11 offers two different |
| 365 | approaches: binding factory functions, and placement-new creation. |
| 366 | |
| 367 | Factory function constructors |
| 368 | ----------------------------- |
| 369 | |
| 370 | It is possible to expose a Python-side constructor from a C++ function that |
| 371 | returns a new object by value or pointer. For example, suppose you have a |
| 372 | class like this: |
| 373 | |
| 374 | .. code-block:: cpp |
| 375 | |
| 376 | class Example { |
| 377 | private: |
| 378 | Example(int); // private constructor |
| 379 | public: |
| 380 | // Factory function: |
| 381 | static Example create(int a) { return Example(a); } |
| 382 | }; |
| 383 | |
| 384 | While it is possible to expose the ``create`` method to Python, it is often |
| 385 | preferrable to expose it on the Python side as a constructor rather than a |
| 386 | named static method. You can do this by calling ``.def(py::init(...))`` with |
| 387 | the function reference returning the new instance passed as an argument. It is |
| 388 | also possible to use this approach to bind a function returning a new instance |
| 389 | by raw pointer or by the holder (e.g. ``std::unique_ptr``). |
| 390 | |
| 391 | The following example shows the different approaches: |
| 392 | |
| 393 | .. code-block:: cpp |
| 394 | |
| 395 | class Example { |
| 396 | private: |
| 397 | Example(int); // private constructor |
| 398 | public: |
| 399 | // Factory function - returned by value: |
| 400 | static Example create(int a) { return Example(a); } |
| 401 | |
| 402 | // These constructors are publicly callable: |
| 403 | Example(double); |
| 404 | Example(int, int); |
| 405 | Example(std::string); |
| 406 | }; |
| 407 | |
| 408 | py::class_<Example>(m, "Example") |
| 409 | // Bind the factory function as a constructor: |
| 410 | .def(py::init(&Example::create)) |
| 411 | // Bind a lambda function returning a pointer wrapped in a holder: |
| 412 | .def(py::init([](std::string arg) { |
| 413 | return std::unique_ptr<Example>(new Example(arg)); |
| 414 | })) |
| 415 | // Return a raw pointer: |
| 416 | .def(py::init([](int a, int b) { return new Example(a, b); })) |
| 417 | // You can mix the above with regular C++ constructor bindings as well: |
| 418 | .def(py::init<double>()) |
| 419 | ; |
| 420 | |
| 421 | When the constructor is invoked from Python, pybind11 will call the factory |
| 422 | function and store the resulting C++ instance in the Python instance. |
| 423 | |
| 424 | When combining factory functions constructors with :ref:`overriding_virtuals` |
| 425 | there are two approaches. The first is to add a constructor to the alias class |
| 426 | that takes a base value by rvalue-reference. If such a constructor is |
| 427 | available, it will be used to construct an alias instance from the value |
| 428 | returned by the factory function. The second option is to provide two factory |
| 429 | functions to ``py::init()``: the first will be invoked when no alias class is |
| 430 | required (i.e. when the class is being used but not inherited from in Python), |
| 431 | and the second will be invoked when an alias is required. |
| 432 | |
| 433 | You can also specify a single factory function that always returns an alias |
| 434 | instance: this will result in behaviour similar to ``py::init_alias<...>()``, |
| 435 | as described in :ref:`extended_aliases`. |
| 436 | |
| 437 | The following example shows the different factory approaches for a class with |
| 438 | an alias: |
| 439 | |
| 440 | .. code-block:: cpp |
| 441 | |
| 442 | #include <pybind11/factory.h> |
| 443 | class Example { |
| 444 | public: |
| 445 | // ... |
| 446 | virtual ~Example() = default; |
| 447 | }; |
| 448 | class PyExample : public Example { |
| 449 | public: |
| 450 | using Example::Example; |
| 451 | PyExample(Example &&base) : Example(std::move(base)) {} |
| 452 | }; |
| 453 | py::class_<Example, PyExample>(m, "Example") |
| 454 | // Returns an Example pointer. If a PyExample is needed, the Example |
| 455 | // instance will be moved via the extra constructor in PyExample, above. |
| 456 | .def(py::init([]() { return new Example(); })) |
| 457 | // Two callbacks: |
| 458 | .def(py::init([]() { return new Example(); } /* no alias needed */, |
| 459 | []() { return new PyExample(); } /* alias needed */)) |
| 460 | // *Always* returns an alias instance (like py::init_alias<>()) |
| 461 | .def(py::init([]() { return new PyExample(); })) |
| 462 | ; |
| 463 | |
| 464 | Low-level placement-new construction |
| 465 | ------------------------------------ |
| 466 | |
| 467 | A second approach for creating new instances use C++ placement new to construct |
| 468 | an object in-place in preallocated memory. To do this, you simply bind a |
| 469 | method name ``__init__`` that takes the class instance as the first argument by |
| 470 | pointer or reference, then uses a placement-new constructor to construct the |
| 471 | object in the pre-allocated (but uninitialized) memory. |
| 472 | |
| 473 | For example, instead of: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 474 | |
| 475 | .. code-block:: cpp |
| 476 | |
| 477 | py::class_<Example>(m, "Example") |
| 478 | .def(py::init<int>()); |
| 479 | |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame^] | 480 | you could equivalently write: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 481 | |
| 482 | .. code-block:: cpp |
| 483 | |
| 484 | py::class_<Example>(m, "Example") |
| 485 | .def("__init__", |
| 486 | [](Example &instance, int arg) { |
| 487 | new (&instance) Example(arg); |
| 488 | } |
| 489 | ); |
| 490 | |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame^] | 491 | which will invoke the constructor in-place at the pre-allocated memory. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 492 | |
| 493 | .. _classes_with_non_public_destructors: |
| 494 | |
| 495 | Non-public destructors |
| 496 | ====================== |
| 497 | |
| 498 | If a class has a private or protected destructor (as might e.g. be the case in |
| 499 | a singleton pattern), a compile error will occur when creating bindings via |
| 500 | pybind11. The underlying issue is that the ``std::unique_ptr`` holder type that |
| 501 | is responsible for managing the lifetime of instances will reference the |
| 502 | destructor even if no deallocations ever take place. In order to expose classes |
| 503 | with private or protected destructors, it is possible to override the holder |
| 504 | type via a holder type argument to ``class_``. Pybind11 provides a helper class |
| 505 | ``py::nodelete`` that disables any destructor invocations. In this case, it is |
| 506 | crucial that instances are deallocated on the C++ side to avoid memory leaks. |
| 507 | |
| 508 | .. code-block:: cpp |
| 509 | |
| 510 | /* ... definition ... */ |
| 511 | |
| 512 | class MyClass { |
| 513 | private: |
| 514 | ~MyClass() { } |
| 515 | }; |
| 516 | |
| 517 | /* ... binding code ... */ |
| 518 | |
| 519 | py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass") |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 520 | .def(py::init<>()) |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 521 | |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 522 | .. _implicit_conversions: |
| 523 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 524 | Implicit conversions |
| 525 | ==================== |
| 526 | |
| 527 | Suppose that instances of two types ``A`` and ``B`` are used in a project, and |
| 528 | that an ``A`` can easily be converted into an instance of type ``B`` (examples of this |
| 529 | could be a fixed and an arbitrary precision number type). |
| 530 | |
| 531 | .. code-block:: cpp |
| 532 | |
| 533 | py::class_<A>(m, "A") |
| 534 | /// ... members ... |
| 535 | |
| 536 | py::class_<B>(m, "B") |
| 537 | .def(py::init<A>()) |
| 538 | /// ... members ... |
| 539 | |
| 540 | m.def("func", |
| 541 | [](const B &) { /* .... */ } |
| 542 | ); |
| 543 | |
| 544 | To invoke the function ``func`` using a variable ``a`` containing an ``A`` |
| 545 | instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++ |
| 546 | will automatically apply an implicit type conversion, which makes it possible |
| 547 | to directly write ``func(a)``. |
| 548 | |
| 549 | In this situation (i.e. where ``B`` has a constructor that converts from |
| 550 | ``A``), the following statement enables similar implicit conversions on the |
| 551 | Python side: |
| 552 | |
| 553 | .. code-block:: cpp |
| 554 | |
| 555 | py::implicitly_convertible<A, B>(); |
| 556 | |
| 557 | .. note:: |
| 558 | |
| 559 | Implicit conversions from ``A`` to ``B`` only work when ``B`` is a custom |
| 560 | data type that is exposed to Python via pybind11. |
| 561 | |
| 562 | .. _static_properties: |
| 563 | |
| 564 | Static properties |
| 565 | ================= |
| 566 | |
| 567 | The section on :ref:`properties` discussed the creation of instance properties |
| 568 | that are implemented in terms of C++ getters and setters. |
| 569 | |
| 570 | Static properties can also be created in a similar way to expose getters and |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 571 | setters of static class attributes. Note that the implicit ``self`` argument |
| 572 | also exists in this case and is used to pass the Python ``type`` subclass |
| 573 | instance. This parameter will often not be needed by the C++ side, and the |
| 574 | following example illustrates how to instantiate a lambda getter function |
| 575 | that ignores it: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 576 | |
| 577 | .. code-block:: cpp |
| 578 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 579 | py::class_<Foo>(m, "Foo") |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 580 | .def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); }); |
| 581 | |
| 582 | Operator overloading |
| 583 | ==================== |
| 584 | |
| 585 | Suppose that we're given the following ``Vector2`` class with a vector addition |
| 586 | and scalar multiplication operation, all implemented using overloaded operators |
| 587 | in C++. |
| 588 | |
| 589 | .. code-block:: cpp |
| 590 | |
| 591 | class Vector2 { |
| 592 | public: |
| 593 | Vector2(float x, float y) : x(x), y(y) { } |
| 594 | |
| 595 | Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); } |
| 596 | Vector2 operator*(float value) const { return Vector2(x * value, y * value); } |
| 597 | Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; } |
| 598 | Vector2& operator*=(float v) { x *= v; y *= v; return *this; } |
| 599 | |
| 600 | friend Vector2 operator*(float f, const Vector2 &v) { |
| 601 | return Vector2(f * v.x, f * v.y); |
| 602 | } |
| 603 | |
| 604 | std::string toString() const { |
| 605 | return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; |
| 606 | } |
| 607 | private: |
| 608 | float x, y; |
| 609 | }; |
| 610 | |
| 611 | The following snippet shows how the above operators can be conveniently exposed |
| 612 | to Python. |
| 613 | |
| 614 | .. code-block:: cpp |
| 615 | |
| 616 | #include <pybind11/operators.h> |
| 617 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 618 | PYBIND11_MODULE(example, m) { |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 619 | py::class_<Vector2>(m, "Vector2") |
| 620 | .def(py::init<float, float>()) |
| 621 | .def(py::self + py::self) |
| 622 | .def(py::self += py::self) |
| 623 | .def(py::self *= float()) |
| 624 | .def(float() * py::self) |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 625 | .def(py::self * float()) |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 626 | .def("__repr__", &Vector2::toString); |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | Note that a line like |
| 630 | |
| 631 | .. code-block:: cpp |
| 632 | |
| 633 | .def(py::self * float()) |
| 634 | |
| 635 | is really just short hand notation for |
| 636 | |
| 637 | .. code-block:: cpp |
| 638 | |
| 639 | .def("__mul__", [](const Vector2 &a, float b) { |
| 640 | return a * b; |
| 641 | }, py::is_operator()) |
| 642 | |
| 643 | This can be useful for exposing additional operators that don't exist on the |
| 644 | C++ side, or to perform other types of customization. The ``py::is_operator`` |
| 645 | flag marker is needed to inform pybind11 that this is an operator, which |
| 646 | returns ``NotImplemented`` when invoked with incompatible arguments rather than |
| 647 | throwing a type error. |
| 648 | |
| 649 | .. note:: |
| 650 | |
| 651 | To use the more convenient ``py::self`` notation, the additional |
| 652 | header file :file:`pybind11/operators.h` must be included. |
| 653 | |
| 654 | .. seealso:: |
| 655 | |
| 656 | The file :file:`tests/test_operator_overloading.cpp` contains a |
| 657 | complete example that demonstrates how to work with overloaded operators in |
| 658 | more detail. |
| 659 | |
| 660 | Pickling support |
| 661 | ================ |
| 662 | |
| 663 | Python's ``pickle`` module provides a powerful facility to serialize and |
| 664 | de-serialize a Python object graph into a binary data stream. To pickle and |
| 665 | unpickle C++ classes using pybind11, two additional functions must be provided. |
| 666 | Suppose the class in question has the following signature: |
| 667 | |
| 668 | .. code-block:: cpp |
| 669 | |
| 670 | class Pickleable { |
| 671 | public: |
| 672 | Pickleable(const std::string &value) : m_value(value) { } |
| 673 | const std::string &value() const { return m_value; } |
| 674 | |
| 675 | void setExtra(int extra) { m_extra = extra; } |
| 676 | int extra() const { return m_extra; } |
| 677 | private: |
| 678 | std::string m_value; |
| 679 | int m_extra = 0; |
| 680 | }; |
| 681 | |
| 682 | The binding code including the requisite ``__setstate__`` and ``__getstate__`` methods [#f3]_ |
| 683 | looks as follows: |
| 684 | |
| 685 | .. code-block:: cpp |
| 686 | |
| 687 | py::class_<Pickleable>(m, "Pickleable") |
| 688 | .def(py::init<std::string>()) |
| 689 | .def("value", &Pickleable::value) |
| 690 | .def("extra", &Pickleable::extra) |
| 691 | .def("setExtra", &Pickleable::setExtra) |
| 692 | .def("__getstate__", [](const Pickleable &p) { |
| 693 | /* Return a tuple that fully encodes the state of the object */ |
| 694 | return py::make_tuple(p.value(), p.extra()); |
| 695 | }) |
| 696 | .def("__setstate__", [](Pickleable &p, py::tuple t) { |
| 697 | if (t.size() != 2) |
| 698 | throw std::runtime_error("Invalid state!"); |
| 699 | |
| 700 | /* Invoke the in-place constructor. Note that this is needed even |
| 701 | when the object just has a trivial default constructor */ |
| 702 | new (&p) Pickleable(t[0].cast<std::string>()); |
| 703 | |
| 704 | /* Assign any additional state */ |
| 705 | p.setExtra(t[1].cast<int>()); |
| 706 | }); |
| 707 | |
| 708 | An instance can now be pickled as follows: |
| 709 | |
| 710 | .. code-block:: python |
| 711 | |
| 712 | try: |
| 713 | import cPickle as pickle # Use cPickle on Python 2.7 |
| 714 | except ImportError: |
| 715 | import pickle |
| 716 | |
| 717 | p = Pickleable("test_value") |
| 718 | p.setExtra(15) |
| 719 | data = pickle.dumps(p, 2) |
| 720 | |
| 721 | Note that only the cPickle module is supported on Python 2.7. The second |
| 722 | argument to ``dumps`` is also crucial: it selects the pickle protocol version |
| 723 | 2, since the older version 1 is not supported. Newer versions are also fineāfor |
| 724 | instance, specify ``-1`` to always use the latest available version. Beware: |
| 725 | failure to follow these instructions will cause important pybind11 memory |
| 726 | allocation routines to be skipped during unpickling, which will likely lead to |
| 727 | memory corruption and/or segmentation faults. |
| 728 | |
| 729 | .. seealso:: |
| 730 | |
| 731 | The file :file:`tests/test_pickling.cpp` contains a complete example |
| 732 | that demonstrates how to pickle and unpickle types using pybind11 in more |
| 733 | detail. |
| 734 | |
| 735 | .. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances |
| 736 | |
| 737 | Multiple Inheritance |
| 738 | ==================== |
| 739 | |
| 740 | pybind11 can create bindings for types that derive from multiple base types |
| 741 | (aka. *multiple inheritance*). To do so, specify all bases in the template |
| 742 | arguments of the ``class_`` declaration: |
| 743 | |
| 744 | .. code-block:: cpp |
| 745 | |
| 746 | py::class_<MyType, BaseType1, BaseType2, BaseType3>(m, "MyType") |
| 747 | ... |
| 748 | |
| 749 | The base types can be specified in arbitrary order, and they can even be |
| 750 | interspersed with alias types and holder types (discussed earlier in this |
| 751 | document)---pybind11 will automatically find out which is which. The only |
| 752 | requirement is that the first template argument is the type to be declared. |
| 753 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 754 | It is also permitted to inherit multiply from exported C++ classes in Python, |
| 755 | as well as inheriting from multiple Python and/or pybind-exported classes. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 756 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 757 | There is one caveat regarding the implementation of this feature: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 758 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 759 | When only one base type is specified for a C++ type that actually has multiple |
| 760 | bases, pybind11 will assume that it does not participate in multiple |
| 761 | inheritance, which can lead to undefined behavior. In such cases, add the tag |
| 762 | ``multiple_inheritance`` to the class constructor: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 763 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 764 | .. code-block:: cpp |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 765 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 766 | py::class_<MyType, BaseType2>(m, "MyType", py::multiple_inheritance()); |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 767 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 768 | The tag is redundant and does not need to be specified when multiple base types |
| 769 | are listed. |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 770 | |
| 771 | .. _module_local: |
| 772 | |
| 773 | Module-local class bindings |
| 774 | =========================== |
| 775 | |
| 776 | When creating a binding for a class, pybind by default makes that binding |
| 777 | "global" across modules. What this means is that a type defined in one module |
| 778 | can be passed to functions of other modules that expect the same C++ type. For |
| 779 | example, this allows the following: |
| 780 | |
| 781 | .. code-block:: cpp |
| 782 | |
| 783 | // In the module1.cpp binding code for module1: |
| 784 | py::class_<Pet>(m, "Pet") |
| 785 | .def(py::init<std::string>()); |
| 786 | |
| 787 | .. code-block:: cpp |
| 788 | |
| 789 | // In the module2.cpp binding code for module2: |
| 790 | m.def("pet_name", [](Pet &p) { return p.name(); }); |
| 791 | |
| 792 | .. code-block:: pycon |
| 793 | |
| 794 | >>> from module1 import Pet |
| 795 | >>> from module2 import pet_name |
| 796 | >>> mypet = Pet("Kitty") |
| 797 | >>> pet_name(mypet) |
| 798 | 'Kitty' |
| 799 | |
| 800 | When writing binding code for a library, this is usually desirable: this |
| 801 | allows, for example, splitting up a complex library into multiple Python |
| 802 | modules. |
| 803 | |
| 804 | In some cases, however, this can cause conflicts. For example, suppose two |
| 805 | unrelated modules make use of an external C++ library and each provide custom |
| 806 | bindings for one of that library's classes. This will result in an error when |
| 807 | a Python program attempts to import both modules (directly or indirectly) |
| 808 | because of conflicting definitions on the external type: |
| 809 | |
| 810 | .. code-block:: cpp |
| 811 | |
| 812 | // dogs.cpp |
| 813 | |
| 814 | // Binding for external library class: |
| 815 | py::class<pets::Pet>(m, "Pet") |
| 816 | .def("name", &pets::Pet::name); |
| 817 | |
| 818 | // Binding for local extension class: |
| 819 | py::class<Dog, pets::Pet>(m, "Dog") |
| 820 | .def(py::init<std::string>()); |
| 821 | |
| 822 | .. code-block:: cpp |
| 823 | |
| 824 | // cats.cpp, in a completely separate project from the above dogs.cpp. |
| 825 | |
| 826 | // Binding for external library class: |
| 827 | py::class<pets::Pet>(m, "Pet") |
| 828 | .def("get_name", &pets::Pet::name); |
| 829 | |
| 830 | // Binding for local extending class: |
| 831 | py::class<Cat, pets::Pet>(m, "Cat") |
| 832 | .def(py::init<std::string>()); |
| 833 | |
| 834 | .. code-block:: pycon |
| 835 | |
| 836 | >>> import cats |
| 837 | >>> import dogs |
| 838 | Traceback (most recent call last): |
| 839 | File "<stdin>", line 1, in <module> |
| 840 | ImportError: generic_type: type "Pet" is already registered! |
| 841 | |
| 842 | To get around this, you can tell pybind11 to keep the external class binding |
| 843 | localized to the module by passing the ``py::module_local()`` attribute into |
| 844 | the ``py::class_`` constructor: |
| 845 | |
| 846 | .. code-block:: cpp |
| 847 | |
| 848 | // Pet binding in dogs.cpp: |
| 849 | py::class<pets::Pet>(m, "Pet", py::module_local()) |
| 850 | .def("name", &pets::Pet::name); |
| 851 | |
| 852 | .. code-block:: cpp |
| 853 | |
| 854 | // Pet binding in cats.cpp: |
| 855 | py::class<pets::Pet>(m, "Pet", py::module_local()) |
| 856 | .def("get_name", &pets::Pet::name); |
| 857 | |
| 858 | This makes the Python-side ``dogs.Pet`` and ``cats.Pet`` into distinct classes |
| 859 | that can only be accepted as ``Pet`` arguments within those classes. This |
| 860 | avoids the conflict and allows both modules to be loaded. |
| 861 | |
| 862 | One limitation of this approach is that because ``py::module_local`` types are |
| 863 | distinct on the Python side, it is not possible to pass such a module-local |
| 864 | type as a C++ ``Pet``-taking function outside that module. For example, if the |
| 865 | above ``cats`` and ``dogs`` module are each extended with a function: |
| 866 | |
| 867 | .. code-block:: cpp |
| 868 | |
| 869 | m.def("petname", [](pets::Pet &p) { return p.name(); }); |
| 870 | |
| 871 | you will only be able to call the function with the local module's class: |
| 872 | |
| 873 | .. code-block:: pycon |
| 874 | |
| 875 | >>> import cats, dogs # No error because of the added py::module_local() |
| 876 | >>> mycat, mydog = cats.Cat("Fluffy"), dogs.Dog("Rover") |
| 877 | >>> (cats.petname(mycat), dogs.petname(mydog)) |
| 878 | ('Fluffy', 'Rover') |
| 879 | >>> cats.petname(mydog) |
| 880 | Traceback (most recent call last): |
| 881 | File "<stdin>", line 1, in <module> |
| 882 | TypeError: petname(): incompatible function arguments. The following argument types are supported: |
| 883 | 1. (arg0: cats.Pet) -> str |
| 884 | |
| 885 | Invoked with: <dogs.Dog object at 0x123> |
| 886 | |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 887 | It is possible to use ``py::module_local()`` registrations in one module even if another module |
| 888 | registers the same type globally: within the module with the module-local definition, all C++ |
| 889 | instances will be cast to the associated bound Python type. Outside the module, any such values |
| 890 | are converted to the global Python type created elsewhere. |
| 891 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 892 | .. note:: |
| 893 | |
| 894 | STL bindings (as provided via the optional :file:`pybind11/stl_bind.h` |
| 895 | header) apply ``py::module_local`` by default when the bound type might |
| 896 | conflict with other modules; see :ref:`stl_bind` for details. |
| 897 | |
| 898 | .. note:: |
| 899 | |
| 900 | The localization of the bound types is actually tied to the shared object |
| 901 | or binary generated by the compiler/linker. For typical modules created |
| 902 | with ``PYBIND11_MODULE()``, this distinction is not significant. It is |
| 903 | possible, however, when :ref:`embedding` to embed multiple modules in the |
| 904 | same binary (see :ref:`embedding_modules`). In such a case, the |
| 905 | localization will apply across all embedded modules within the same binary. |
| 906 | |
| 907 | .. seealso:: |
| 908 | |
| 909 | The file :file:`tests/test_local_bindings.cpp` contains additional examples |
| 910 | that demonstrate how ``py::module_local()`` works. |