blob: d1f8849b4b821b0b5c8fdb4ba0e6128adcef8696 [file] [log] [blame]
Dean Moldovan67b52d82016-10-16 19:12:43 +02001Classes
2#######
3
4This section presents advanced binding code for classes and it is assumed
5that you are already familiar with the basics from :doc:`/classes`.
6
7.. _overriding_virtuals:
8
9Overriding virtual functions in Python
10======================================
11
12Suppose that a C++ class or interface has a virtual function that we'd like to
13to override from within Python (we'll focus on the class ``Animal``; ``Dog`` is
14given as a specific example of how one would do this with traditional C++
15code).
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
35Let's also suppose that we are given a plain function which calls the
36function ``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
44Normally, the binding code for these classes would look as follows:
45
46.. code-block:: cpp
47
Dean Moldovan443ab592017-04-24 01:51:44 +020048 PYBIND11_MODULE(example, m) {
François Beckerce9d6e22018-05-07 15:18:08 +020049 py::class_<Animal>(m, "Animal")
Dean Moldovan67b52d82016-10-16 19:12:43 +020050 .def("go", &Animal::go);
51
Tom de Geusa7ff6162018-05-04 17:04:45 +020052 py::class_<Dog, Animal>(m, "Dog")
Dean Moldovan67b52d82016-10-16 19:12:43 +020053 .def(py::init<>());
54
55 m.def("call_go", &call_go);
Dean Moldovan67b52d82016-10-16 19:12:43 +020056 }
57
58However, these bindings are impossible to extend: ``Animal`` is not
59constructible, and we clearly require some kind of "trampoline" that
60redirects virtual calls back to Python.
61
62Defining a new type of ``Animal`` from within Python is possible but requires a
63helper class that is defined as follows:
64
65.. code-block:: cpp
66
67 class PyAnimal : public Animal {
68 public:
69 /* Inherit the constructors */
70 using Animal::Animal;
71
72 /* Trampoline (need one for each virtual function) */
73 std::string go(int n_times) override {
74 PYBIND11_OVERLOAD_PURE(
75 std::string, /* Return type */
76 Animal, /* Parent class */
jbarlow837830e852017-01-13 02:17:29 -080077 go, /* Name of function in C++ (must match Python name) */
Dean Moldovan67b52d82016-10-16 19:12:43 +020078 n_times /* Argument(s) */
79 );
80 }
81 };
82
Ivor Wanders2b045752019-06-10 16:12:28 -040083The macro :c:macro:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
84functions, and :c:macro:`PYBIND11_OVERLOAD` should be used for functions which have
85a default implementation. There are also two alternate macros
86:c:macro:`PYBIND11_OVERLOAD_PURE_NAME` and :c:macro:`PYBIND11_OVERLOAD_NAME` which
Dean Moldovan67b52d82016-10-16 19:12:43 +020087take a string-valued name argument between the *Parent class* and *Name of the
Dean Moldovan234f7c32017-08-17 17:03:46 +020088function* slots, which defines the name of function in Python. This is required
jbarlow837830e852017-01-13 02:17:29 -080089when the C++ and Python versions of the
Dean Moldovan67b52d82016-10-16 19:12:43 +020090function have different names, e.g. ``operator()`` vs ``__call__``.
91
92The binding code also needs a few minor adaptations (highlighted):
93
94.. code-block:: cpp
Tom de Geusa7ff6162018-05-04 17:04:45 +020095 :emphasize-lines: 2,3
Dean Moldovan67b52d82016-10-16 19:12:43 +020096
Dean Moldovan443ab592017-04-24 01:51:44 +020097 PYBIND11_MODULE(example, m) {
François Beckerce9d6e22018-05-07 15:18:08 +020098 py::class_<Animal, PyAnimal /* <--- trampoline*/>(m, "Animal")
Dean Moldovan67b52d82016-10-16 19:12:43 +020099 .def(py::init<>())
100 .def("go", &Animal::go);
101
Tom de Geusa7ff6162018-05-04 17:04:45 +0200102 py::class_<Dog, Animal>(m, "Dog")
Dean Moldovan67b52d82016-10-16 19:12:43 +0200103 .def(py::init<>());
104
105 m.def("call_go", &call_go);
Dean Moldovan67b52d82016-10-16 19:12:43 +0200106 }
107
108Importantly, pybind11 is made aware of the trampoline helper class by
jbarlow837830e852017-01-13 02:17:29 -0800109specifying it as an extra template argument to :class:`class_`. (This can also
Dean Moldovan67b52d82016-10-16 19:12:43 +0200110be combined with other template arguments such as a custom holder type; the
111order of template types does not matter). Following this, we are able to
112define a constructor as usual.
113
jbarlow837830e852017-01-13 02:17:29 -0800114Bindings should be made against the actual class, not the trampoline helper class.
115
116.. code-block:: cpp
Tom de Geusa7ff6162018-05-04 17:04:45 +0200117 :emphasize-lines: 3
jbarlow837830e852017-01-13 02:17:29 -0800118
Tom de Geusa7ff6162018-05-04 17:04:45 +0200119 py::class_<Animal, PyAnimal /* <--- trampoline*/>(m, "Animal");
120 .def(py::init<>())
121 .def("go", &PyAnimal::go); /* <--- THIS IS WRONG, use &Animal::go */
jbarlow837830e852017-01-13 02:17:29 -0800122
Dean Moldovan67b52d82016-10-16 19:12:43 +0200123Note, however, that the above is sufficient for allowing python classes to
EricCousineau-TRIe06077b2017-08-07 18:37:42 -0400124extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the
Dean Moldovan67b52d82016-10-16 19:12:43 +0200125necessary steps required to providing proper overload support for inherited
126classes.
127
128The Python session below shows how to override ``Animal::go`` and invoke it via
129a virtual method call.
130
131.. code-block:: pycon
132
133 >>> from example import *
134 >>> d = Dog()
135 >>> call_go(d)
136 u'woof! woof! woof! '
137 >>> class Cat(Animal):
138 ... def go(self, n_times):
139 ... return "meow! " * n_times
140 ...
141 >>> c = Cat()
142 >>> call_go(c)
143 u'meow! meow! meow! '
144
EricCousineau-TRIe06077b2017-08-07 18:37:42 -0400145If you are defining a custom constructor in a derived Python class, you *must*
146ensure that you explicitly call the bound C++ constructor using ``__init__``,
147*regardless* of whether it is a default constructor or not. Otherwise, the
148memory for the C++ portion of the instance will be left uninitialized, which
149will generally leave the C++ instance in an invalid state and cause undefined
150behavior if the C++ instance is subsequently used.
151
152Here is an example:
153
154.. code-block:: python
155
Manuel Schneider492da592019-06-10 22:02:58 +0200156 class Dachshund(Dog):
EricCousineau-TRIe06077b2017-08-07 18:37:42 -0400157 def __init__(self, name):
Tom de Geusa7ff6162018-05-04 17:04:45 +0200158 Dog.__init__(self) # Without this, undefined behavior may occur if the C++ portions are referenced.
EricCousineau-TRIe06077b2017-08-07 18:37:42 -0400159 self.name = name
160 def bark(self):
161 return "yap!"
162
163Note that a direct ``__init__`` constructor *should be called*, and ``super()``
164should not be used. For simple cases of linear inheritance, ``super()``
165may work, but once you begin mixing Python and C++ multiple inheritance,
166things will fall apart due to differences between Python's MRO and C++'s
167mechanisms.
168
Dean Moldovan67b52d82016-10-16 19:12:43 +0200169Please take a look at the :ref:`macro_notes` before using this feature.
170
171.. note::
172
173 When the overridden type returns a reference or pointer to a type that
174 pybind11 converts from Python (for example, numeric values, std::string,
175 and other built-in value-converting types), there are some limitations to
176 be aware of:
177
178 - because in these cases there is no C++ variable to reference (the value
179 is stored in the referenced Python variable), pybind11 provides one in
180 the PYBIND11_OVERLOAD macros (when needed) with static storage duration.
181 Note that this means that invoking the overloaded method on *any*
182 instance will change the referenced value stored in *all* instances of
183 that type.
184
185 - Attempts to modify a non-const reference will not have the desired
186 effect: it will change only the static cache variable, but this change
187 will not propagate to underlying Python instance, and the change will be
188 replaced the next time the overload is invoked.
189
190.. seealso::
191
192 The file :file:`tests/test_virtual_functions.cpp` contains a complete
193 example that demonstrates how to override virtual functions using pybind11
194 in more detail.
195
196.. _virtual_and_inheritance:
197
198Combining virtual functions and inheritance
199===========================================
200
201When combining virtual methods with inheritance, you need to be sure to provide
202an override for each method for which you want to allow overrides from derived
203python classes. For example, suppose we extend the above ``Animal``/``Dog``
204example as follows:
205
206.. code-block:: cpp
207
208 class Animal {
209 public:
210 virtual std::string go(int n_times) = 0;
211 virtual std::string name() { return "unknown"; }
212 };
myd73499b815ad2017-01-13 18:15:52 +0800213 class Dog : public Animal {
Dean Moldovan67b52d82016-10-16 19:12:43 +0200214 public:
215 std::string go(int n_times) override {
216 std::string result;
217 for (int i=0; i<n_times; ++i)
218 result += bark() + " ";
219 return result;
220 }
221 virtual std::string bark() { return "woof!"; }
222 };
223
224then the trampoline class for ``Animal`` must, as described in the previous
225section, override ``go()`` and ``name()``, but in order to allow python code to
226inherit properly from ``Dog``, we also need a trampoline class for ``Dog`` that
227overrides both the added ``bark()`` method *and* the ``go()`` and ``name()``
228methods inherited from ``Animal`` (even though ``Dog`` doesn't directly
229override the ``name()`` method):
230
231.. code-block:: cpp
232
233 class PyAnimal : public Animal {
234 public:
235 using Animal::Animal; // Inherit constructors
236 std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Animal, go, n_times); }
237 std::string name() override { PYBIND11_OVERLOAD(std::string, Animal, name, ); }
238 };
239 class PyDog : public Dog {
240 public:
241 using Dog::Dog; // Inherit constructors
Omar Awileac6cb912019-06-10 21:56:17 +0200242 std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, Dog, go, n_times); }
Dean Moldovan67b52d82016-10-16 19:12:43 +0200243 std::string name() override { PYBIND11_OVERLOAD(std::string, Dog, name, ); }
244 std::string bark() override { PYBIND11_OVERLOAD(std::string, Dog, bark, ); }
245 };
246
Wenzel Jakobab262592017-03-22 21:39:19 +0100247.. note::
248
249 Note the trailing commas in the ``PYBIND11_OVERLOAD`` calls to ``name()``
250 and ``bark()``. These are needed to portably implement a trampoline for a
251 function that does not take any arguments. For functions that take
252 a nonzero number of arguments, the trailing comma must be omitted.
253
Dean Moldovan67b52d82016-10-16 19:12:43 +0200254A registered class derived from a pybind11-registered class with virtual
255methods requires a similar trampoline class, *even if* it doesn't explicitly
256declare or override any virtual methods itself:
257
258.. code-block:: cpp
259
260 class Husky : public Dog {};
261 class PyHusky : public Husky {
myd73499b815ad2017-01-13 18:15:52 +0800262 public:
263 using Husky::Husky; // Inherit constructors
Dean Moldovan67b52d82016-10-16 19:12:43 +0200264 std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
265 std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
266 std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
267 };
268
269There is, however, a technique that can be used to avoid this duplication
270(which can be especially helpful for a base class with several virtual
271methods). The technique involves using template trampoline classes, as
272follows:
273
274.. code-block:: cpp
275
276 template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
myd73499b815ad2017-01-13 18:15:52 +0800277 public:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200278 using AnimalBase::AnimalBase; // Inherit constructors
279 std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
280 std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
281 };
282 template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
myd73499b815ad2017-01-13 18:15:52 +0800283 public:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200284 using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
285 // Override PyAnimal's pure virtual go() with a non-pure one:
286 std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
287 std::string bark() override { PYBIND11_OVERLOAD(std::string, DogBase, bark, ); }
288 };
289
290This technique has the advantage of requiring just one trampoline method to be
291declared per virtual method and pure virtual method override. It does,
292however, require the compiler to generate at least as many methods (and
293possibly more, if both pure virtual and overridden pure virtual methods are
294exposed, as above).
295
296The classes are then registered with pybind11 using:
297
298.. code-block:: cpp
299
300 py::class_<Animal, PyAnimal<>> animal(m, "Animal");
301 py::class_<Dog, PyDog<>> dog(m, "Dog");
302 py::class_<Husky, PyDog<Husky>> husky(m, "Husky");
303 // ... add animal, dog, husky definitions
304
305Note that ``Husky`` did not require a dedicated trampoline template class at
306all, since it neither declares any new virtual methods nor provides any pure
307virtual method implementations.
308
309With either the repeated-virtuals or templated trampoline methods in place, you
310can now create a python class that inherits from ``Dog``:
311
312.. code-block:: python
313
314 class ShihTzu(Dog):
315 def bark(self):
316 return "yip!"
317
318.. seealso::
319
320 See the file :file:`tests/test_virtual_functions.cpp` for complete examples
321 using both the duplication and templated trampoline approaches.
322
Jason Rhinelander464d9892017-06-12 21:52:48 -0400323.. _extended_aliases:
324
Dean Moldovan67b52d82016-10-16 19:12:43 +0200325Extended trampoline class functionality
326=======================================
327
Ivor Wanders2b045752019-06-10 16:12:28 -0400328.. _extended_class_functionality_forced_trampoline
329
330Forced trampoline class initialisation
331--------------------------------------
Dean Moldovan67b52d82016-10-16 19:12:43 +0200332The trampoline classes described in the previous sections are, by default, only
333initialized when needed. More specifically, they are initialized when a python
334class actually inherits from a registered type (instead of merely creating an
335instance of the registered type), or when a registered constructor is only
336valid for the trampoline class but not the registered class. This is primarily
337for performance reasons: when the trampoline class is not needed for anything
338except virtual method dispatching, not initializing the trampoline class
339improves performance by avoiding needing to do a run-time check to see if the
340inheriting python instance has an overloaded method.
341
342Sometimes, however, it is useful to always initialize a trampoline class as an
343intermediate class that does more than just handle virtual method dispatching.
344For example, such a class might perform extra class initialization, extra
345destruction operations, and might define new members and methods to enable a
346more python-like interface to a class.
347
348In order to tell pybind11 that it should *always* initialize the trampoline
349class when creating new instances of a type, the class constructors should be
350declared using ``py::init_alias<Args, ...>()`` instead of the usual
351``py::init<Args, ...>()``. This forces construction via the trampoline class,
352ensuring member initialization and (eventual) destruction.
353
354.. seealso::
355
Dean Moldovan0bc272b2017-06-22 23:42:11 +0200356 See the file :file:`tests/test_virtual_functions.cpp` for complete examples
Dean Moldovan67b52d82016-10-16 19:12:43 +0200357 showing both normal and forced trampoline instantiation.
358
Ivor Wanders2b045752019-06-10 16:12:28 -0400359Different method signatures
360---------------------------
361The macro's introduced in :ref:`overriding_virtuals` cover most of the standard
362use cases when exposing C++ classes to Python. Sometimes it is hard or unwieldy
363to create a direct one-on-one mapping between the arguments and method return
364type.
365
366An example would be when the C++ signature contains output arguments using
367references (See also :ref:`faq_reference_arguments`). Another way of solving
368this is to use the method body of the trampoline class to do conversions to the
369input and return of the Python method.
370
371The main building block to do so is the :func:`get_overload`, this function
372allows retrieving a method implemented in Python from within the trampoline's
373methods. Consider for example a C++ method which has the signature
374``bool myMethod(int32_t& value)``, where the return indicates whether
375something should be done with the ``value``. This can be made convenient on the
376Python side by allowing the Python function to return ``None`` or an ``int``:
377
378.. code-block:: cpp
379
380 bool MyClass::myMethod(int32_t& value)
381 {
382 pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
383 // Try to look up the overloaded method on the Python side.
384 pybind11::function overload = pybind11::get_overload(this, "myMethod");
385 if (overload) { // method is found
386 auto obj = overload(value); // Call the Python function.
387 if (py::isinstance<py::int_>(obj)) { // check if it returned a Python integer type
388 value = obj.cast<int32_t>(); // Cast it and assign it to the value.
389 return true; // Return true; value should be used.
390 } else {
391 return false; // Python returned none, return false.
392 }
393 }
394 return false; // Alternatively return MyClass::myMethod(value);
395 }
396
397
Dean Moldovan67b52d82016-10-16 19:12:43 +0200398.. _custom_constructors:
399
400Custom constructors
401===================
402
403The syntax for binding constructors was previously introduced, but it only
Jason Rhinelander464d9892017-06-12 21:52:48 -0400404works when a constructor of the appropriate arguments actually exists on the
Dean Moldovan0991d7f2017-09-05 16:49:33 +0200405C++ side. To extend this to more general cases, pybind11 makes it possible
406to bind factory functions as constructors. For example, suppose you have a
Jason Rhinelander464d9892017-06-12 21:52:48 -0400407class like this:
408
409.. code-block:: cpp
410
411 class Example {
412 private:
413 Example(int); // private constructor
414 public:
415 // Factory function:
416 static Example create(int a) { return Example(a); }
417 };
418
Dean Moldovan0991d7f2017-09-05 16:49:33 +0200419 py::class_<Example>(m, "Example")
420 .def(py::init(&Example::create));
421
Wenzel Jakobfb276c62017-08-22 00:55:53 +0200422While it is possible to create a straightforward binding of the static
423``create`` method, it may sometimes be preferable to expose it as a constructor
424on the Python side. This can be accomplished by calling ``.def(py::init(...))``
425with the function reference returning the new instance passed as an argument.
426It is also possible to use this approach to bind a function returning a new
427instance by raw pointer or by the holder (e.g. ``std::unique_ptr``).
Jason Rhinelander464d9892017-06-12 21:52:48 -0400428
429The following example shows the different approaches:
430
431.. code-block:: cpp
432
433 class Example {
434 private:
435 Example(int); // private constructor
436 public:
437 // Factory function - returned by value:
438 static Example create(int a) { return Example(a); }
439
440 // These constructors are publicly callable:
441 Example(double);
442 Example(int, int);
443 Example(std::string);
444 };
445
446 py::class_<Example>(m, "Example")
447 // Bind the factory function as a constructor:
448 .def(py::init(&Example::create))
449 // Bind a lambda function returning a pointer wrapped in a holder:
450 .def(py::init([](std::string arg) {
451 return std::unique_ptr<Example>(new Example(arg));
452 }))
453 // Return a raw pointer:
454 .def(py::init([](int a, int b) { return new Example(a, b); }))
455 // You can mix the above with regular C++ constructor bindings as well:
456 .def(py::init<double>())
457 ;
458
459When the constructor is invoked from Python, pybind11 will call the factory
460function and store the resulting C++ instance in the Python instance.
461
Wenzel Jakobfb276c62017-08-22 00:55:53 +0200462When combining factory functions constructors with :ref:`virtual function
463trampolines <overriding_virtuals>` there are two approaches. The first is to
464add a constructor to the alias class that takes a base value by
465rvalue-reference. If such a constructor is available, it will be used to
466construct an alias instance from the value returned by the factory function.
467The second option is to provide two factory functions to ``py::init()``: the
468first will be invoked when no alias class is required (i.e. when the class is
469being used but not inherited from in Python), and the second will be invoked
470when an alias is required.
Jason Rhinelander464d9892017-06-12 21:52:48 -0400471
472You can also specify a single factory function that always returns an alias
473instance: this will result in behaviour similar to ``py::init_alias<...>()``,
Wenzel Jakobfb276c62017-08-22 00:55:53 +0200474as described in the :ref:`extended trampoline class documentation
475<extended_aliases>`.
Jason Rhinelander464d9892017-06-12 21:52:48 -0400476
477The following example shows the different factory approaches for a class with
478an alias:
479
480.. code-block:: cpp
481
482 #include <pybind11/factory.h>
483 class Example {
484 public:
485 // ...
486 virtual ~Example() = default;
487 };
488 class PyExample : public Example {
489 public:
490 using Example::Example;
491 PyExample(Example &&base) : Example(std::move(base)) {}
492 };
493 py::class_<Example, PyExample>(m, "Example")
494 // Returns an Example pointer. If a PyExample is needed, the Example
495 // instance will be moved via the extra constructor in PyExample, above.
496 .def(py::init([]() { return new Example(); }))
497 // Two callbacks:
498 .def(py::init([]() { return new Example(); } /* no alias needed */,
499 []() { return new PyExample(); } /* alias needed */))
500 // *Always* returns an alias instance (like py::init_alias<>())
501 .def(py::init([]() { return new PyExample(); }))
502 ;
503
Wenzel Jakob4336a7d2017-08-21 22:48:28 +0200504Brace initialization
505--------------------
506
507``pybind11::init<>`` internally uses C++11 brace initialization to call the
508constructor of the target class. This means that it can be used to bind
509*implicit* constructors as well:
510
511.. code-block:: cpp
512
513 struct Aggregate {
514 int a;
515 std::string b;
516 };
517
518 py::class_<Aggregate>(m, "Aggregate")
519 .def(py::init<int, const std::string &>());
520
521.. note::
522
523 Note that brace initialization preferentially invokes constructor overloads
524 taking a ``std::initializer_list``. In the rare event that this causes an
525 issue, you can work around it by using ``py::init(...)`` with a lambda
526 function that constructs the new object as desired.
527
Dean Moldovan67b52d82016-10-16 19:12:43 +0200528.. _classes_with_non_public_destructors:
529
530Non-public destructors
531======================
532
533If a class has a private or protected destructor (as might e.g. be the case in
534a singleton pattern), a compile error will occur when creating bindings via
535pybind11. The underlying issue is that the ``std::unique_ptr`` holder type that
536is responsible for managing the lifetime of instances will reference the
537destructor even if no deallocations ever take place. In order to expose classes
538with private or protected destructors, it is possible to override the holder
539type via a holder type argument to ``class_``. Pybind11 provides a helper class
540``py::nodelete`` that disables any destructor invocations. In this case, it is
541crucial that instances are deallocated on the C++ side to avoid memory leaks.
542
543.. code-block:: cpp
544
545 /* ... definition ... */
546
547 class MyClass {
548 private:
549 ~MyClass() { }
550 };
551
552 /* ... binding code ... */
553
554 py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
myd73499b815ad2017-01-13 18:15:52 +0800555 .def(py::init<>())
Dean Moldovan67b52d82016-10-16 19:12:43 +0200556
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500557.. _implicit_conversions:
558
Dean Moldovan67b52d82016-10-16 19:12:43 +0200559Implicit conversions
560====================
561
562Suppose that instances of two types ``A`` and ``B`` are used in a project, and
563that an ``A`` can easily be converted into an instance of type ``B`` (examples of this
564could be a fixed and an arbitrary precision number type).
565
566.. code-block:: cpp
567
568 py::class_<A>(m, "A")
569 /// ... members ...
570
571 py::class_<B>(m, "B")
572 .def(py::init<A>())
573 /// ... members ...
574
575 m.def("func",
576 [](const B &) { /* .... */ }
577 );
578
579To invoke the function ``func`` using a variable ``a`` containing an ``A``
580instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++
581will automatically apply an implicit type conversion, which makes it possible
582to directly write ``func(a)``.
583
584In this situation (i.e. where ``B`` has a constructor that converts from
585``A``), the following statement enables similar implicit conversions on the
586Python side:
587
588.. code-block:: cpp
589
590 py::implicitly_convertible<A, B>();
591
592.. note::
593
594 Implicit conversions from ``A`` to ``B`` only work when ``B`` is a custom
595 data type that is exposed to Python via pybind11.
596
Wenzel Jakob8ed5b8a2017-08-28 16:34:06 +0200597 To prevent runaway recursion, implicit conversions are non-reentrant: an
598 implicit conversion invoked as part of another implicit conversion of the
599 same type (i.e. from ``A`` to ``B``) will fail.
600
Dean Moldovan67b52d82016-10-16 19:12:43 +0200601.. _static_properties:
602
603Static properties
604=================
605
606The section on :ref:`properties` discussed the creation of instance properties
607that are implemented in terms of C++ getters and setters.
608
609Static properties can also be created in a similar way to expose getters and
Dean Moldovandd016652017-02-16 23:02:56 +0100610setters of static class attributes. Note that the implicit ``self`` argument
611also exists in this case and is used to pass the Python ``type`` subclass
612instance. This parameter will often not be needed by the C++ side, and the
613following example illustrates how to instantiate a lambda getter function
614that ignores it:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200615
616.. code-block:: cpp
617
Dean Moldovandd016652017-02-16 23:02:56 +0100618 py::class_<Foo>(m, "Foo")
Dean Moldovan67b52d82016-10-16 19:12:43 +0200619 .def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); });
620
621Operator overloading
622====================
623
624Suppose that we're given the following ``Vector2`` class with a vector addition
625and scalar multiplication operation, all implemented using overloaded operators
626in C++.
627
628.. code-block:: cpp
629
630 class Vector2 {
631 public:
632 Vector2(float x, float y) : x(x), y(y) { }
633
634 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
635 Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
636 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
637 Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
638
639 friend Vector2 operator*(float f, const Vector2 &v) {
640 return Vector2(f * v.x, f * v.y);
641 }
642
643 std::string toString() const {
644 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
645 }
646 private:
647 float x, y;
648 };
649
650The following snippet shows how the above operators can be conveniently exposed
651to Python.
652
653.. code-block:: cpp
654
655 #include <pybind11/operators.h>
656
Dean Moldovan443ab592017-04-24 01:51:44 +0200657 PYBIND11_MODULE(example, m) {
Dean Moldovan67b52d82016-10-16 19:12:43 +0200658 py::class_<Vector2>(m, "Vector2")
659 .def(py::init<float, float>())
660 .def(py::self + py::self)
661 .def(py::self += py::self)
662 .def(py::self *= float())
663 .def(float() * py::self)
myd73499b815ad2017-01-13 18:15:52 +0800664 .def(py::self * float())
Dean Moldovan67b52d82016-10-16 19:12:43 +0200665 .def("__repr__", &Vector2::toString);
Dean Moldovan67b52d82016-10-16 19:12:43 +0200666 }
667
668Note that a line like
669
670.. code-block:: cpp
671
672 .def(py::self * float())
673
674is really just short hand notation for
675
676.. code-block:: cpp
677
678 .def("__mul__", [](const Vector2 &a, float b) {
679 return a * b;
680 }, py::is_operator())
681
682This can be useful for exposing additional operators that don't exist on the
683C++ side, or to perform other types of customization. The ``py::is_operator``
684flag marker is needed to inform pybind11 that this is an operator, which
685returns ``NotImplemented`` when invoked with incompatible arguments rather than
686throwing a type error.
687
688.. note::
689
690 To use the more convenient ``py::self`` notation, the additional
691 header file :file:`pybind11/operators.h` must be included.
692
693.. seealso::
694
695 The file :file:`tests/test_operator_overloading.cpp` contains a
696 complete example that demonstrates how to work with overloaded operators in
697 more detail.
698
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200699.. _pickling:
700
Dean Moldovan67b52d82016-10-16 19:12:43 +0200701Pickling support
702================
703
704Python's ``pickle`` module provides a powerful facility to serialize and
705de-serialize a Python object graph into a binary data stream. To pickle and
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200706unpickle C++ classes using pybind11, a ``py::pickle()`` definition must be
707provided. Suppose the class in question has the following signature:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200708
709.. code-block:: cpp
710
711 class Pickleable {
712 public:
713 Pickleable(const std::string &value) : m_value(value) { }
714 const std::string &value() const { return m_value; }
715
716 void setExtra(int extra) { m_extra = extra; }
717 int extra() const { return m_extra; }
718 private:
719 std::string m_value;
720 int m_extra = 0;
721 };
722
Patrik Huber1ad22272017-09-04 22:00:19 +0100723Pickling support in Python is enabled by defining the ``__setstate__`` and
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200724``__getstate__`` methods [#f3]_. For pybind11 classes, use ``py::pickle()``
725to bind these two functions:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200726
727.. code-block:: cpp
728
729 py::class_<Pickleable>(m, "Pickleable")
730 .def(py::init<std::string>())
731 .def("value", &Pickleable::value)
732 .def("extra", &Pickleable::extra)
733 .def("setExtra", &Pickleable::setExtra)
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200734 .def(py::pickle(
735 [](const Pickleable &p) { // __getstate__
736 /* Return a tuple that fully encodes the state of the object */
737 return py::make_tuple(p.value(), p.extra());
738 },
739 [](py::tuple t) { // __setstate__
740 if (t.size() != 2)
741 throw std::runtime_error("Invalid state!");
Dean Moldovan67b52d82016-10-16 19:12:43 +0200742
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200743 /* Create a new C++ instance */
744 Pickleable p(t[0].cast<std::string>());
Dean Moldovan67b52d82016-10-16 19:12:43 +0200745
Dean Moldovan1e5a7da2017-08-24 01:53:15 +0200746 /* Assign any additional state */
747 p.setExtra(t[1].cast<int>());
748
749 return p;
750 }
751 ));
752
753The ``__setstate__`` part of the ``py::picke()`` definition follows the same
754rules as the single-argument version of ``py::init()``. The return type can be
755a value, pointer or holder type. See :ref:`custom_constructors` for details.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200756
757An instance can now be pickled as follows:
758
759.. code-block:: python
760
761 try:
762 import cPickle as pickle # Use cPickle on Python 2.7
763 except ImportError:
764 import pickle
765
766 p = Pickleable("test_value")
767 p.setExtra(15)
768 data = pickle.dumps(p, 2)
769
770Note that only the cPickle module is supported on Python 2.7. The second
771argument to ``dumps`` is also crucial: it selects the pickle protocol version
7722, since the older version 1 is not supported. Newer versions are also fine—for
773instance, specify ``-1`` to always use the latest available version. Beware:
774failure to follow these instructions will cause important pybind11 memory
775allocation routines to be skipped during unpickling, which will likely lead to
776memory corruption and/or segmentation faults.
777
778.. seealso::
779
780 The file :file:`tests/test_pickling.cpp` contains a complete example
781 that demonstrates how to pickle and unpickle types using pybind11 in more
782 detail.
783
784.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
785
786Multiple Inheritance
787====================
788
789pybind11 can create bindings for types that derive from multiple base types
790(aka. *multiple inheritance*). To do so, specify all bases in the template
791arguments of the ``class_`` declaration:
792
793.. code-block:: cpp
794
795 py::class_<MyType, BaseType1, BaseType2, BaseType3>(m, "MyType")
796 ...
797
798The base types can be specified in arbitrary order, and they can even be
799interspersed with alias types and holder types (discussed earlier in this
800document)---pybind11 will automatically find out which is which. The only
801requirement is that the first template argument is the type to be declared.
802
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500803It is also permitted to inherit multiply from exported C++ classes in Python,
Tom de Geusa7ff6162018-05-04 17:04:45 +0200804as well as inheriting from multiple Python and/or pybind11-exported classes.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200805
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500806There is one caveat regarding the implementation of this feature:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200807
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500808When only one base type is specified for a C++ type that actually has multiple
809bases, pybind11 will assume that it does not participate in multiple
810inheritance, which can lead to undefined behavior. In such cases, add the tag
811``multiple_inheritance`` to the class constructor:
Dean Moldovan67b52d82016-10-16 19:12:43 +0200812
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500813.. code-block:: cpp
Dean Moldovan67b52d82016-10-16 19:12:43 +0200814
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500815 py::class_<MyType, BaseType2>(m, "MyType", py::multiple_inheritance());
Dean Moldovan67b52d82016-10-16 19:12:43 +0200816
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500817The tag is redundant and does not need to be specified when multiple base types
818are listed.
Jason Rhinelander7437c692017-07-28 22:03:44 -0400819
820.. _module_local:
821
822Module-local class bindings
823===========================
824
Tom de Geusa7ff6162018-05-04 17:04:45 +0200825When creating a binding for a class, pybind11 by default makes that binding
Jason Rhinelander7437c692017-07-28 22:03:44 -0400826"global" across modules. What this means is that a type defined in one module
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400827can be returned from any module resulting in the same Python type. For
Jason Rhinelander7437c692017-07-28 22:03:44 -0400828example, this allows the following:
829
830.. code-block:: cpp
831
832 // In the module1.cpp binding code for module1:
833 py::class_<Pet>(m, "Pet")
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400834 .def(py::init<std::string>())
835 .def_readonly("name", &Pet::name);
Jason Rhinelander7437c692017-07-28 22:03:44 -0400836
837.. code-block:: cpp
838
839 // In the module2.cpp binding code for module2:
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400840 m.def("create_pet", [](std::string name) { return new Pet(name); });
Jason Rhinelander7437c692017-07-28 22:03:44 -0400841
842.. code-block:: pycon
843
844 >>> from module1 import Pet
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400845 >>> from module2 import create_pet
846 >>> pet1 = Pet("Kitty")
847 >>> pet2 = create_pet("Doggy")
848 >>> pet2.name()
849 'Doggy'
Jason Rhinelander7437c692017-07-28 22:03:44 -0400850
851When writing binding code for a library, this is usually desirable: this
852allows, for example, splitting up a complex library into multiple Python
853modules.
854
855In some cases, however, this can cause conflicts. For example, suppose two
856unrelated modules make use of an external C++ library and each provide custom
857bindings for one of that library's classes. This will result in an error when
858a Python program attempts to import both modules (directly or indirectly)
859because of conflicting definitions on the external type:
860
861.. code-block:: cpp
862
863 // dogs.cpp
864
865 // Binding for external library class:
866 py::class<pets::Pet>(m, "Pet")
867 .def("name", &pets::Pet::name);
868
869 // Binding for local extension class:
870 py::class<Dog, pets::Pet>(m, "Dog")
871 .def(py::init<std::string>());
872
873.. code-block:: cpp
874
875 // cats.cpp, in a completely separate project from the above dogs.cpp.
876
877 // Binding for external library class:
878 py::class<pets::Pet>(m, "Pet")
879 .def("get_name", &pets::Pet::name);
880
881 // Binding for local extending class:
882 py::class<Cat, pets::Pet>(m, "Cat")
883 .def(py::init<std::string>());
884
885.. code-block:: pycon
886
887 >>> import cats
888 >>> import dogs
889 Traceback (most recent call last):
890 File "<stdin>", line 1, in <module>
891 ImportError: generic_type: type "Pet" is already registered!
892
893To get around this, you can tell pybind11 to keep the external class binding
894localized to the module by passing the ``py::module_local()`` attribute into
895the ``py::class_`` constructor:
896
897.. code-block:: cpp
898
899 // Pet binding in dogs.cpp:
900 py::class<pets::Pet>(m, "Pet", py::module_local())
901 .def("name", &pets::Pet::name);
902
903.. code-block:: cpp
904
905 // Pet binding in cats.cpp:
906 py::class<pets::Pet>(m, "Pet", py::module_local())
907 .def("get_name", &pets::Pet::name);
908
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400909This makes the Python-side ``dogs.Pet`` and ``cats.Pet`` into distinct classes,
910avoiding the conflict and allowing both modules to be loaded. C++ code in the
911``dogs`` module that casts or returns a ``Pet`` instance will result in a
912``dogs.Pet`` Python instance, while C++ code in the ``cats`` module will result
913in a ``cats.Pet`` Python instance.
Jason Rhinelander7437c692017-07-28 22:03:44 -0400914
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400915This does come with two caveats, however: First, external modules cannot return
916or cast a ``Pet`` instance to Python (unless they also provide their own local
917bindings). Second, from the Python point of view they are two distinct classes.
918
919Note that the locality only applies in the C++ -> Python direction. When
920passing such a ``py::module_local`` type into a C++ function, the module-local
921classes are still considered. This means that if the following function is
922added to any module (including but not limited to the ``cats`` and ``dogs``
923modules above) it will be callable with either a ``dogs.Pet`` or ``cats.Pet``
924argument:
Jason Rhinelander7437c692017-07-28 22:03:44 -0400925
926.. code-block:: cpp
927
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400928 m.def("pet_name", [](const pets::Pet &pet) { return pet.name(); });
Jason Rhinelander7437c692017-07-28 22:03:44 -0400929
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400930For example, suppose the above function is added to each of ``cats.cpp``,
931``dogs.cpp`` and ``frogs.cpp`` (where ``frogs.cpp`` is some other module that
932does *not* bind ``Pets`` at all).
Jason Rhinelander7437c692017-07-28 22:03:44 -0400933
934.. code-block:: pycon
935
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400936 >>> import cats, dogs, frogs # No error because of the added py::module_local()
Jason Rhinelander7437c692017-07-28 22:03:44 -0400937 >>> mycat, mydog = cats.Cat("Fluffy"), dogs.Dog("Rover")
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400938 >>> (cats.pet_name(mycat), dogs.pet_name(mydog))
Jason Rhinelander7437c692017-07-28 22:03:44 -0400939 ('Fluffy', 'Rover')
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400940 >>> (cats.pet_name(mydog), dogs.pet_name(mycat), frogs.pet_name(mycat))
941 ('Rover', 'Fluffy', 'Fluffy')
Jason Rhinelander7437c692017-07-28 22:03:44 -0400942
Jason Rhinelander5e14aa62017-08-17 11:38:05 -0400943It is possible to use ``py::module_local()`` registrations in one module even
944if another module registers the same type globally: within the module with the
945module-local definition, all C++ instances will be cast to the associated bound
946Python type. In other modules any such values are converted to the global
947Python type created elsewhere.
Jason Rhinelander4b159232017-08-04 13:05:12 -0400948
Jason Rhinelander7437c692017-07-28 22:03:44 -0400949.. note::
950
951 STL bindings (as provided via the optional :file:`pybind11/stl_bind.h`
952 header) apply ``py::module_local`` by default when the bound type might
953 conflict with other modules; see :ref:`stl_bind` for details.
954
955.. note::
956
957 The localization of the bound types is actually tied to the shared object
958 or binary generated by the compiler/linker. For typical modules created
959 with ``PYBIND11_MODULE()``, this distinction is not significant. It is
960 possible, however, when :ref:`embedding` to embed multiple modules in the
961 same binary (see :ref:`embedding_modules`). In such a case, the
962 localization will apply across all embedded modules within the same binary.
963
964.. seealso::
965
966 The file :file:`tests/test_local_bindings.cpp` contains additional examples
967 that demonstrate how ``py::module_local()`` works.
Dean Moldovan234f7c32017-08-17 17:03:46 +0200968
969Binding protected member functions
970==================================
971
972It's normally not possible to expose ``protected`` member functions to Python:
973
974.. code-block:: cpp
975
976 class A {
977 protected:
978 int foo() const { return 42; }
979 };
980
981 py::class_<A>(m, "A")
982 .def("foo", &A::foo); // error: 'foo' is a protected member of 'A'
983
984On one hand, this is good because non-``public`` members aren't meant to be
985accessed from the outside. But we may want to make use of ``protected``
986functions in derived Python classes.
987
988The following pattern makes this possible:
989
990.. code-block:: cpp
991
992 class A {
993 protected:
994 int foo() const { return 42; }
995 };
996
997 class Publicist : public A { // helper type for exposing protected functions
998 public:
999 using A::foo; // inherited with different access modifier
1000 };
1001
1002 py::class_<A>(m, "A") // bind the primary class
1003 .def("foo", &Publicist::foo); // expose protected methods via the publicist
1004
1005This works because ``&Publicist::foo`` is exactly the same function as
1006``&A::foo`` (same signature and address), just with a different access
1007modifier. The only purpose of the ``Publicist`` helper class is to make
1008the function name ``public``.
1009
1010If the intent is to expose ``protected`` ``virtual`` functions which can be
1011overridden in Python, the publicist pattern can be combined with the previously
1012described trampoline:
1013
1014.. code-block:: cpp
1015
1016 class A {
1017 public:
1018 virtual ~A() = default;
1019
1020 protected:
1021 virtual int foo() const { return 42; }
1022 };
1023
1024 class Trampoline : public A {
1025 public:
1026 int foo() const override { PYBIND11_OVERLOAD(int, A, foo, ); }
1027 };
1028
1029 class Publicist : public A {
1030 public:
1031 using A::foo;
1032 };
1033
1034 py::class_<A, Trampoline>(m, "A") // <-- `Trampoline` here
1035 .def("foo", &Publicist::foo); // <-- `Publicist` here, not `Trampoline`!
1036
1037.. note::
1038
1039 MSVC 2015 has a compiler bug (fixed in version 2017) which
1040 requires a more explicit function binding in the form of
1041 ``.def("foo", static_cast<int (A::*)() const>(&Publicist::foo));``
1042 where ``int (A::*)() const`` is the type of ``A::foo``.
oremanjfd9bc8f2018-04-13 20:13:10 -04001043
1044Custom automatic downcasters
1045============================
1046
1047As explained in :ref:`inheritance`, pybind11 comes with built-in
1048understanding of the dynamic type of polymorphic objects in C++; that
1049is, returning a Pet to Python produces a Python object that knows it's
1050wrapping a Dog, if Pet has virtual methods and pybind11 knows about
1051Dog and this Pet is in fact a Dog. Sometimes, you might want to
1052provide this automatic downcasting behavior when creating bindings for
1053a class hierarchy that does not use standard C++ polymorphism, such as
1054LLVM [#f4]_. As long as there's some way to determine at runtime
1055whether a downcast is safe, you can proceed by specializing the
1056``pybind11::polymorphic_type_hook`` template:
1057
1058.. code-block:: cpp
1059
1060 enum class PetKind { Cat, Dog, Zebra };
1061 struct Pet { // Not polymorphic: has no virtual methods
1062 const PetKind kind;
1063 int age = 0;
1064 protected:
1065 Pet(PetKind _kind) : kind(_kind) {}
1066 };
1067 struct Dog : Pet {
1068 Dog() : Pet(PetKind::Dog) {}
1069 std::string sound = "woof!";
1070 std::string bark() const { return sound; }
1071 };
1072
1073 namespace pybind11 {
1074 template<> struct polymorphic_type_hook<Pet> {
1075 static const void *get(const Pet *src, const std::type_info*& type) {
1076 // note that src may be nullptr
1077 if (src && src->kind == PetKind::Dog) {
1078 type = &typeid(Dog);
1079 return static_cast<const Dog*>(src);
1080 }
1081 return src;
1082 }
1083 };
1084 } // namespace pybind11
1085
1086When pybind11 wants to convert a C++ pointer of type ``Base*`` to a
1087Python object, it calls ``polymorphic_type_hook<Base>::get()`` to
1088determine if a downcast is possible. The ``get()`` function should use
1089whatever runtime information is available to determine if its ``src``
1090parameter is in fact an instance of some class ``Derived`` that
1091inherits from ``Base``. If it finds such a ``Derived``, it sets ``type
1092= &typeid(Derived)`` and returns a pointer to the ``Derived`` object
1093that contains ``src``. Otherwise, it just returns ``src``, leaving
1094``type`` at its default value of nullptr. If you set ``type`` to a
1095type that pybind11 doesn't know about, no downcasting will occur, and
1096the original ``src`` pointer will be used with its static type
1097``Base*``.
1098
1099It is critical that the returned pointer and ``type`` argument of
1100``get()`` agree with each other: if ``type`` is set to something
1101non-null, the returned pointer must point to the start of an object
1102whose type is ``type``. If the hierarchy being exposed uses only
1103single inheritance, a simple ``return src;`` will achieve this just
1104fine, but in the general case, you must cast ``src`` to the
1105appropriate derived-class pointer (e.g. using
1106``static_cast<Derived>(src)``) before allowing it to be returned as a
1107``void*``.
1108
1109.. [#f4] https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html
1110
1111.. note::
1112
1113 pybind11's standard support for downcasting objects whose types
1114 have virtual methods is implemented using
1115 ``polymorphic_type_hook`` too, using the standard C++ ability to
1116 determine the most-derived type of a polymorphic object using
1117 ``typeid()`` and to cast a base pointer to that most-derived type
1118 (even if you don't know what it is) using ``dynamic_cast<void*>``.
1119
1120.. seealso::
1121
1122 The file :file:`tests/test_tagbased_polymorphic.cpp` contains a
1123 more complete example, including a demonstration of how to provide
1124 automatic downcasting for an entire class hierarchy without
1125 writing one get() function for each class.