github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/docs/advanced/classes.rst (about) 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 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 48 PYBIND11_MODULE(example, m) { 49 py::class_<Animal>(m, "Animal") 50 .def("go", &Animal::go); 51 52 py::class_<Dog, Animal>(m, "Dog") 53 .def(py::init<>()); 54 55 m.def("call_go", &call_go); 56 } 57 58 However, these bindings are impossible to extend: ``Animal`` is not 59 constructible, and we clearly require some kind of "trampoline" that 60 redirects virtual calls back to Python. 61 62 Defining a new type of ``Animal`` from within Python is possible but requires a 63 helper 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_OVERRIDE_PURE( 75 std::string, /* Return type */ 76 Animal, /* Parent class */ 77 go, /* Name of function in C++ (must match Python name) */ 78 n_times /* Argument(s) */ 79 ); 80 } 81 }; 82 83 The macro :c:macro:`PYBIND11_OVERRIDE_PURE` should be used for pure virtual 84 functions, and :c:macro:`PYBIND11_OVERRIDE` should be used for functions which have 85 a default implementation. There are also two alternate macros 86 :c:macro:`PYBIND11_OVERRIDE_PURE_NAME` and :c:macro:`PYBIND11_OVERRIDE_NAME` which 87 take a string-valued name argument between the *Parent class* and *Name of the 88 function* slots, which defines the name of function in Python. This is required 89 when the C++ and Python versions of the 90 function have different names, e.g. ``operator()`` vs ``__call__``. 91 92 The binding code also needs a few minor adaptations (highlighted): 93 94 .. code-block:: cpp 95 :emphasize-lines: 2,3 96 97 PYBIND11_MODULE(example, m) { 98 py::class_<Animal, PyAnimal /* <--- trampoline*/>(m, "Animal") 99 .def(py::init<>()) 100 .def("go", &Animal::go); 101 102 py::class_<Dog, Animal>(m, "Dog") 103 .def(py::init<>()); 104 105 m.def("call_go", &call_go); 106 } 107 108 Importantly, pybind11 is made aware of the trampoline helper class by 109 specifying it as an extra template argument to :class:`class_`. (This can also 110 be combined with other template arguments such as a custom holder type; the 111 order of template types does not matter). Following this, we are able to 112 define a constructor as usual. 113 114 Bindings should be made against the actual class, not the trampoline helper class. 115 116 .. code-block:: cpp 117 :emphasize-lines: 3 118 119 py::class_<Animal, PyAnimal /* <--- trampoline*/>(m, "Animal"); 120 .def(py::init<>()) 121 .def("go", &PyAnimal::go); /* <--- THIS IS WRONG, use &Animal::go */ 122 123 Note, however, that the above is sufficient for allowing python classes to 124 extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the 125 necessary steps required to providing proper overriding support for inherited 126 classes. 127 128 The Python session below shows how to override ``Animal::go`` and invoke it via 129 a virtual method call. 130 131 .. code-block:: pycon 132 133 >>> from example import * 134 >>> d = Dog() 135 >>> call_go(d) 136 '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 'meow! meow! meow! ' 144 145 If you are defining a custom constructor in a derived Python class, you *must* 146 ensure that you explicitly call the bound C++ constructor using ``__init__``, 147 *regardless* of whether it is a default constructor or not. Otherwise, the 148 memory for the C++ portion of the instance will be left uninitialized, which 149 will generally leave the C++ instance in an invalid state and cause undefined 150 behavior if the C++ instance is subsequently used. 151 152 .. versionchanged:: 2.6 153 The default pybind11 metaclass will throw a ``TypeError`` when it detects 154 that ``__init__`` was not called by a derived class. 155 156 Here is an example: 157 158 .. code-block:: python 159 160 class Dachshund(Dog): 161 def __init__(self, name): 162 Dog.__init__(self) # Without this, a TypeError is raised. 163 self.name = name 164 165 def bark(self): 166 return "yap!" 167 168 Note that a direct ``__init__`` constructor *should be called*, and ``super()`` 169 should not be used. For simple cases of linear inheritance, ``super()`` 170 may work, but once you begin mixing Python and C++ multiple inheritance, 171 things will fall apart due to differences between Python's MRO and C++'s 172 mechanisms. 173 174 Please take a look at the :ref:`macro_notes` before using this feature. 175 176 .. note:: 177 178 When the overridden type returns a reference or pointer to a type that 179 pybind11 converts from Python (for example, numeric values, std::string, 180 and other built-in value-converting types), there are some limitations to 181 be aware of: 182 183 - because in these cases there is no C++ variable to reference (the value 184 is stored in the referenced Python variable), pybind11 provides one in 185 the PYBIND11_OVERRIDE macros (when needed) with static storage duration. 186 Note that this means that invoking the overridden method on *any* 187 instance will change the referenced value stored in *all* instances of 188 that type. 189 190 - Attempts to modify a non-const reference will not have the desired 191 effect: it will change only the static cache variable, but this change 192 will not propagate to underlying Python instance, and the change will be 193 replaced the next time the override is invoked. 194 195 .. warning:: 196 197 The :c:macro:`PYBIND11_OVERRIDE` and accompanying macros used to be called 198 ``PYBIND11_OVERLOAD`` up until pybind11 v2.5.0, and :func:`get_override` 199 used to be called ``get_overload``. This naming was corrected and the older 200 macro and function names may soon be deprecated, in order to reduce 201 confusion with overloaded functions and methods and ``py::overload_cast`` 202 (see :ref:`classes`). 203 204 .. seealso:: 205 206 The file :file:`tests/test_virtual_functions.cpp` contains a complete 207 example that demonstrates how to override virtual functions using pybind11 208 in more detail. 209 210 .. _virtual_and_inheritance: 211 212 Combining virtual functions and inheritance 213 =========================================== 214 215 When combining virtual methods with inheritance, you need to be sure to provide 216 an override for each method for which you want to allow overrides from derived 217 python classes. For example, suppose we extend the above ``Animal``/``Dog`` 218 example as follows: 219 220 .. code-block:: cpp 221 222 class Animal { 223 public: 224 virtual std::string go(int n_times) = 0; 225 virtual std::string name() { return "unknown"; } 226 }; 227 class Dog : public Animal { 228 public: 229 std::string go(int n_times) override { 230 std::string result; 231 for (int i=0; i<n_times; ++i) 232 result += bark() + " "; 233 return result; 234 } 235 virtual std::string bark() { return "woof!"; } 236 }; 237 238 then the trampoline class for ``Animal`` must, as described in the previous 239 section, override ``go()`` and ``name()``, but in order to allow python code to 240 inherit properly from ``Dog``, we also need a trampoline class for ``Dog`` that 241 overrides both the added ``bark()`` method *and* the ``go()`` and ``name()`` 242 methods inherited from ``Animal`` (even though ``Dog`` doesn't directly 243 override the ``name()`` method): 244 245 .. code-block:: cpp 246 247 class PyAnimal : public Animal { 248 public: 249 using Animal::Animal; // Inherit constructors 250 std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Animal, go, n_times); } 251 std::string name() override { PYBIND11_OVERRIDE(std::string, Animal, name, ); } 252 }; 253 class PyDog : public Dog { 254 public: 255 using Dog::Dog; // Inherit constructors 256 std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, Dog, go, n_times); } 257 std::string name() override { PYBIND11_OVERRIDE(std::string, Dog, name, ); } 258 std::string bark() override { PYBIND11_OVERRIDE(std::string, Dog, bark, ); } 259 }; 260 261 .. note:: 262 263 Note the trailing commas in the ``PYBIND11_OVERRIDE`` calls to ``name()`` 264 and ``bark()``. These are needed to portably implement a trampoline for a 265 function that does not take any arguments. For functions that take 266 a nonzero number of arguments, the trailing comma must be omitted. 267 268 A registered class derived from a pybind11-registered class with virtual 269 methods requires a similar trampoline class, *even if* it doesn't explicitly 270 declare or override any virtual methods itself: 271 272 .. code-block:: cpp 273 274 class Husky : public Dog {}; 275 class PyHusky : public Husky { 276 public: 277 using Husky::Husky; // Inherit constructors 278 std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Husky, go, n_times); } 279 std::string name() override { PYBIND11_OVERRIDE(std::string, Husky, name, ); } 280 std::string bark() override { PYBIND11_OVERRIDE(std::string, Husky, bark, ); } 281 }; 282 283 There is, however, a technique that can be used to avoid this duplication 284 (which can be especially helpful for a base class with several virtual 285 methods). The technique involves using template trampoline classes, as 286 follows: 287 288 .. code-block:: cpp 289 290 template <class AnimalBase = Animal> class PyAnimal : public AnimalBase { 291 public: 292 using AnimalBase::AnimalBase; // Inherit constructors 293 std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, AnimalBase, go, n_times); } 294 std::string name() override { PYBIND11_OVERRIDE(std::string, AnimalBase, name, ); } 295 }; 296 template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> { 297 public: 298 using PyAnimal<DogBase>::PyAnimal; // Inherit constructors 299 // Override PyAnimal's pure virtual go() with a non-pure one: 300 std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, DogBase, go, n_times); } 301 std::string bark() override { PYBIND11_OVERRIDE(std::string, DogBase, bark, ); } 302 }; 303 304 This technique has the advantage of requiring just one trampoline method to be 305 declared per virtual method and pure virtual method override. It does, 306 however, require the compiler to generate at least as many methods (and 307 possibly more, if both pure virtual and overridden pure virtual methods are 308 exposed, as above). 309 310 The classes are then registered with pybind11 using: 311 312 .. code-block:: cpp 313 314 py::class_<Animal, PyAnimal<>> animal(m, "Animal"); 315 py::class_<Dog, Animal, PyDog<>> dog(m, "Dog"); 316 py::class_<Husky, Dog, PyDog<Husky>> husky(m, "Husky"); 317 // ... add animal, dog, husky definitions 318 319 Note that ``Husky`` did not require a dedicated trampoline template class at 320 all, since it neither declares any new virtual methods nor provides any pure 321 virtual method implementations. 322 323 With either the repeated-virtuals or templated trampoline methods in place, you 324 can now create a python class that inherits from ``Dog``: 325 326 .. code-block:: python 327 328 class ShihTzu(Dog): 329 def bark(self): 330 return "yip!" 331 332 .. seealso:: 333 334 See the file :file:`tests/test_virtual_functions.cpp` for complete examples 335 using both the duplication and templated trampoline approaches. 336 337 .. _extended_aliases: 338 339 Extended trampoline class functionality 340 ======================================= 341 342 .. _extended_class_functionality_forced_trampoline: 343 344 Forced trampoline class initialisation 345 -------------------------------------- 346 The trampoline classes described in the previous sections are, by default, only 347 initialized when needed. More specifically, they are initialized when a python 348 class actually inherits from a registered type (instead of merely creating an 349 instance of the registered type), or when a registered constructor is only 350 valid for the trampoline class but not the registered class. This is primarily 351 for performance reasons: when the trampoline class is not needed for anything 352 except virtual method dispatching, not initializing the trampoline class 353 improves performance by avoiding needing to do a run-time check to see if the 354 inheriting python instance has an overridden method. 355 356 Sometimes, however, it is useful to always initialize a trampoline class as an 357 intermediate class that does more than just handle virtual method dispatching. 358 For example, such a class might perform extra class initialization, extra 359 destruction operations, and might define new members and methods to enable a 360 more python-like interface to a class. 361 362 In order to tell pybind11 that it should *always* initialize the trampoline 363 class when creating new instances of a type, the class constructors should be 364 declared using ``py::init_alias<Args, ...>()`` instead of the usual 365 ``py::init<Args, ...>()``. This forces construction via the trampoline class, 366 ensuring member initialization and (eventual) destruction. 367 368 .. seealso:: 369 370 See the file :file:`tests/test_virtual_functions.cpp` for complete examples 371 showing both normal and forced trampoline instantiation. 372 373 Different method signatures 374 --------------------------- 375 The macro's introduced in :ref:`overriding_virtuals` cover most of the standard 376 use cases when exposing C++ classes to Python. Sometimes it is hard or unwieldy 377 to create a direct one-on-one mapping between the arguments and method return 378 type. 379 380 An example would be when the C++ signature contains output arguments using 381 references (See also :ref:`faq_reference_arguments`). Another way of solving 382 this is to use the method body of the trampoline class to do conversions to the 383 input and return of the Python method. 384 385 The main building block to do so is the :func:`get_override`, this function 386 allows retrieving a method implemented in Python from within the trampoline's 387 methods. Consider for example a C++ method which has the signature 388 ``bool myMethod(int32_t& value)``, where the return indicates whether 389 something should be done with the ``value``. This can be made convenient on the 390 Python side by allowing the Python function to return ``None`` or an ``int``: 391 392 .. code-block:: cpp 393 394 bool MyClass::myMethod(int32_t& value) 395 { 396 pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope. 397 // Try to look up the overridden method on the Python side. 398 pybind11::function override = pybind11::get_override(this, "myMethod"); 399 if (override) { // method is found 400 auto obj = override(value); // Call the Python function. 401 if (py::isinstance<py::int_>(obj)) { // check if it returned a Python integer type 402 value = obj.cast<int32_t>(); // Cast it and assign it to the value. 403 return true; // Return true; value should be used. 404 } else { 405 return false; // Python returned none, return false. 406 } 407 } 408 return false; // Alternatively return MyClass::myMethod(value); 409 } 410 411 412 .. _custom_constructors: 413 414 Custom constructors 415 =================== 416 417 The syntax for binding constructors was previously introduced, but it only 418 works when a constructor of the appropriate arguments actually exists on the 419 C++ side. To extend this to more general cases, pybind11 makes it possible 420 to bind factory functions as constructors. For example, suppose you have a 421 class like this: 422 423 .. code-block:: cpp 424 425 class Example { 426 private: 427 Example(int); // private constructor 428 public: 429 // Factory function: 430 static Example create(int a) { return Example(a); } 431 }; 432 433 py::class_<Example>(m, "Example") 434 .def(py::init(&Example::create)); 435 436 While it is possible to create a straightforward binding of the static 437 ``create`` method, it may sometimes be preferable to expose it as a constructor 438 on the Python side. This can be accomplished by calling ``.def(py::init(...))`` 439 with the function reference returning the new instance passed as an argument. 440 It is also possible to use this approach to bind a function returning a new 441 instance by raw pointer or by the holder (e.g. ``std::unique_ptr``). 442 443 The following example shows the different approaches: 444 445 .. code-block:: cpp 446 447 class Example { 448 private: 449 Example(int); // private constructor 450 public: 451 // Factory function - returned by value: 452 static Example create(int a) { return Example(a); } 453 454 // These constructors are publicly callable: 455 Example(double); 456 Example(int, int); 457 Example(std::string); 458 }; 459 460 py::class_<Example>(m, "Example") 461 // Bind the factory function as a constructor: 462 .def(py::init(&Example::create)) 463 // Bind a lambda function returning a pointer wrapped in a holder: 464 .def(py::init([](std::string arg) { 465 return std::unique_ptr<Example>(new Example(arg)); 466 })) 467 // Return a raw pointer: 468 .def(py::init([](int a, int b) { return new Example(a, b); })) 469 // You can mix the above with regular C++ constructor bindings as well: 470 .def(py::init<double>()) 471 ; 472 473 When the constructor is invoked from Python, pybind11 will call the factory 474 function and store the resulting C++ instance in the Python instance. 475 476 When combining factory functions constructors with :ref:`virtual function 477 trampolines <overriding_virtuals>` there are two approaches. The first is to 478 add a constructor to the alias class that takes a base value by 479 rvalue-reference. If such a constructor is available, it will be used to 480 construct an alias instance from the value returned by the factory function. 481 The second option is to provide two factory functions to ``py::init()``: the 482 first will be invoked when no alias class is required (i.e. when the class is 483 being used but not inherited from in Python), and the second will be invoked 484 when an alias is required. 485 486 You can also specify a single factory function that always returns an alias 487 instance: this will result in behaviour similar to ``py::init_alias<...>()``, 488 as described in the :ref:`extended trampoline class documentation 489 <extended_aliases>`. 490 491 The following example shows the different factory approaches for a class with 492 an alias: 493 494 .. code-block:: cpp 495 496 #include <pybind11/factory.h> 497 class Example { 498 public: 499 // ... 500 virtual ~Example() = default; 501 }; 502 class PyExample : public Example { 503 public: 504 using Example::Example; 505 PyExample(Example &&base) : Example(std::move(base)) {} 506 }; 507 py::class_<Example, PyExample>(m, "Example") 508 // Returns an Example pointer. If a PyExample is needed, the Example 509 // instance will be moved via the extra constructor in PyExample, above. 510 .def(py::init([]() { return new Example(); })) 511 // Two callbacks: 512 .def(py::init([]() { return new Example(); } /* no alias needed */, 513 []() { return new PyExample(); } /* alias needed */)) 514 // *Always* returns an alias instance (like py::init_alias<>()) 515 .def(py::init([]() { return new PyExample(); })) 516 ; 517 518 Brace initialization 519 -------------------- 520 521 ``pybind11::init<>`` internally uses C++11 brace initialization to call the 522 constructor of the target class. This means that it can be used to bind 523 *implicit* constructors as well: 524 525 .. code-block:: cpp 526 527 struct Aggregate { 528 int a; 529 std::string b; 530 }; 531 532 py::class_<Aggregate>(m, "Aggregate") 533 .def(py::init<int, const std::string &>()); 534 535 .. note:: 536 537 Note that brace initialization preferentially invokes constructor overloads 538 taking a ``std::initializer_list``. In the rare event that this causes an 539 issue, you can work around it by using ``py::init(...)`` with a lambda 540 function that constructs the new object as desired. 541 542 .. _classes_with_non_public_destructors: 543 544 Non-public destructors 545 ====================== 546 547 If a class has a private or protected destructor (as might e.g. be the case in 548 a singleton pattern), a compile error will occur when creating bindings via 549 pybind11. The underlying issue is that the ``std::unique_ptr`` holder type that 550 is responsible for managing the lifetime of instances will reference the 551 destructor even if no deallocations ever take place. In order to expose classes 552 with private or protected destructors, it is possible to override the holder 553 type via a holder type argument to ``class_``. Pybind11 provides a helper class 554 ``py::nodelete`` that disables any destructor invocations. In this case, it is 555 crucial that instances are deallocated on the C++ side to avoid memory leaks. 556 557 .. code-block:: cpp 558 559 /* ... definition ... */ 560 561 class MyClass { 562 private: 563 ~MyClass() { } 564 }; 565 566 /* ... binding code ... */ 567 568 py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass") 569 .def(py::init<>()) 570 571 .. _destructors_that_call_python: 572 573 Destructors that call Python 574 ============================ 575 576 If a Python function is invoked from a C++ destructor, an exception may be thrown 577 of type :class:`error_already_set`. If this error is thrown out of a class destructor, 578 ``std::terminate()`` will be called, terminating the process. Class destructors 579 must catch all exceptions of type :class:`error_already_set` to discard the Python 580 exception using :func:`error_already_set::discard_as_unraisable`. 581 582 Every Python function should be treated as *possibly throwing*. When a Python generator 583 stops yielding items, Python will throw a ``StopIteration`` exception, which can pass 584 though C++ destructors if the generator's stack frame holds the last reference to C++ 585 objects. 586 587 For more information, see :ref:`the documentation on exceptions <unraisable_exceptions>`. 588 589 .. code-block:: cpp 590 591 class MyClass { 592 public: 593 ~MyClass() { 594 try { 595 py::print("Even printing is dangerous in a destructor"); 596 py::exec("raise ValueError('This is an unraisable exception')"); 597 } catch (py::error_already_set &e) { 598 // error_context should be information about where/why the occurred, 599 // e.g. use __func__ to get the name of the current function 600 e.discard_as_unraisable(__func__); 601 } 602 } 603 }; 604 605 .. note:: 606 607 pybind11 does not support C++ destructors marked ``noexcept(false)``. 608 609 .. versionadded:: 2.6 610 611 .. _implicit_conversions: 612 613 Implicit conversions 614 ==================== 615 616 Suppose that instances of two types ``A`` and ``B`` are used in a project, and 617 that an ``A`` can easily be converted into an instance of type ``B`` (examples of this 618 could be a fixed and an arbitrary precision number type). 619 620 .. code-block:: cpp 621 622 py::class_<A>(m, "A") 623 /// ... members ... 624 625 py::class_<B>(m, "B") 626 .def(py::init<A>()) 627 /// ... members ... 628 629 m.def("func", 630 [](const B &) { /* .... */ } 631 ); 632 633 To invoke the function ``func`` using a variable ``a`` containing an ``A`` 634 instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++ 635 will automatically apply an implicit type conversion, which makes it possible 636 to directly write ``func(a)``. 637 638 In this situation (i.e. where ``B`` has a constructor that converts from 639 ``A``), the following statement enables similar implicit conversions on the 640 Python side: 641 642 .. code-block:: cpp 643 644 py::implicitly_convertible<A, B>(); 645 646 .. note:: 647 648 Implicit conversions from ``A`` to ``B`` only work when ``B`` is a custom 649 data type that is exposed to Python via pybind11. 650 651 To prevent runaway recursion, implicit conversions are non-reentrant: an 652 implicit conversion invoked as part of another implicit conversion of the 653 same type (i.e. from ``A`` to ``B``) will fail. 654 655 .. _static_properties: 656 657 Static properties 658 ================= 659 660 The section on :ref:`properties` discussed the creation of instance properties 661 that are implemented in terms of C++ getters and setters. 662 663 Static properties can also be created in a similar way to expose getters and 664 setters of static class attributes. Note that the implicit ``self`` argument 665 also exists in this case and is used to pass the Python ``type`` subclass 666 instance. This parameter will often not be needed by the C++ side, and the 667 following example illustrates how to instantiate a lambda getter function 668 that ignores it: 669 670 .. code-block:: cpp 671 672 py::class_<Foo>(m, "Foo") 673 .def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); }); 674 675 Operator overloading 676 ==================== 677 678 Suppose that we're given the following ``Vector2`` class with a vector addition 679 and scalar multiplication operation, all implemented using overloaded operators 680 in C++. 681 682 .. code-block:: cpp 683 684 class Vector2 { 685 public: 686 Vector2(float x, float y) : x(x), y(y) { } 687 688 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); } 689 Vector2 operator*(float value) const { return Vector2(x * value, y * value); } 690 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; } 691 Vector2& operator*=(float v) { x *= v; y *= v; return *this; } 692 693 friend Vector2 operator*(float f, const Vector2 &v) { 694 return Vector2(f * v.x, f * v.y); 695 } 696 697 std::string toString() const { 698 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; 699 } 700 private: 701 float x, y; 702 }; 703 704 The following snippet shows how the above operators can be conveniently exposed 705 to Python. 706 707 .. code-block:: cpp 708 709 #include <pybind11/operators.h> 710 711 PYBIND11_MODULE(example, m) { 712 py::class_<Vector2>(m, "Vector2") 713 .def(py::init<float, float>()) 714 .def(py::self + py::self) 715 .def(py::self += py::self) 716 .def(py::self *= float()) 717 .def(float() * py::self) 718 .def(py::self * float()) 719 .def(-py::self) 720 .def("__repr__", &Vector2::toString); 721 } 722 723 Note that a line like 724 725 .. code-block:: cpp 726 727 .def(py::self * float()) 728 729 is really just short hand notation for 730 731 .. code-block:: cpp 732 733 .def("__mul__", [](const Vector2 &a, float b) { 734 return a * b; 735 }, py::is_operator()) 736 737 This can be useful for exposing additional operators that don't exist on the 738 C++ side, or to perform other types of customization. The ``py::is_operator`` 739 flag marker is needed to inform pybind11 that this is an operator, which 740 returns ``NotImplemented`` when invoked with incompatible arguments rather than 741 throwing a type error. 742 743 .. note:: 744 745 To use the more convenient ``py::self`` notation, the additional 746 header file :file:`pybind11/operators.h` must be included. 747 748 .. seealso:: 749 750 The file :file:`tests/test_operator_overloading.cpp` contains a 751 complete example that demonstrates how to work with overloaded operators in 752 more detail. 753 754 .. _pickling: 755 756 Pickling support 757 ================ 758 759 Python's ``pickle`` module provides a powerful facility to serialize and 760 de-serialize a Python object graph into a binary data stream. To pickle and 761 unpickle C++ classes using pybind11, a ``py::pickle()`` definition must be 762 provided. Suppose the class in question has the following signature: 763 764 .. code-block:: cpp 765 766 class Pickleable { 767 public: 768 Pickleable(const std::string &value) : m_value(value) { } 769 const std::string &value() const { return m_value; } 770 771 void setExtra(int extra) { m_extra = extra; } 772 int extra() const { return m_extra; } 773 private: 774 std::string m_value; 775 int m_extra = 0; 776 }; 777 778 Pickling support in Python is enabled by defining the ``__setstate__`` and 779 ``__getstate__`` methods [#f3]_. For pybind11 classes, use ``py::pickle()`` 780 to bind these two functions: 781 782 .. code-block:: cpp 783 784 py::class_<Pickleable>(m, "Pickleable") 785 .def(py::init<std::string>()) 786 .def("value", &Pickleable::value) 787 .def("extra", &Pickleable::extra) 788 .def("setExtra", &Pickleable::setExtra) 789 .def(py::pickle( 790 [](const Pickleable &p) { // __getstate__ 791 /* Return a tuple that fully encodes the state of the object */ 792 return py::make_tuple(p.value(), p.extra()); 793 }, 794 [](py::tuple t) { // __setstate__ 795 if (t.size() != 2) 796 throw std::runtime_error("Invalid state!"); 797 798 /* Create a new C++ instance */ 799 Pickleable p(t[0].cast<std::string>()); 800 801 /* Assign any additional state */ 802 p.setExtra(t[1].cast<int>()); 803 804 return p; 805 } 806 )); 807 808 The ``__setstate__`` part of the ``py::pickle()`` definition follows the same 809 rules as the single-argument version of ``py::init()``. The return type can be 810 a value, pointer or holder type. See :ref:`custom_constructors` for details. 811 812 An instance can now be pickled as follows: 813 814 .. code-block:: python 815 816 import pickle 817 818 p = Pickleable("test_value") 819 p.setExtra(15) 820 data = pickle.dumps(p) 821 822 823 .. note:: 824 If given, the second argument to ``dumps`` must be 2 or larger - 0 and 1 are 825 not supported. Newer versions are also fine; for instance, specify ``-1`` to 826 always use the latest available version. Beware: failure to follow these 827 instructions will cause important pybind11 memory allocation routines to be 828 skipped during unpickling, which will likely lead to memory corruption 829 and/or segmentation faults. Python defaults to version 3 (Python 3-3.7) and 830 version 4 for Python 3.8+. 831 832 .. seealso:: 833 834 The file :file:`tests/test_pickling.cpp` contains a complete example 835 that demonstrates how to pickle and unpickle types using pybind11 in more 836 detail. 837 838 .. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances 839 840 Deepcopy support 841 ================ 842 843 Python normally uses references in assignments. Sometimes a real copy is needed 844 to prevent changing all copies. The ``copy`` module [#f5]_ provides these 845 capabilities. 846 847 A class with pickle support is automatically also (deep)copy 848 compatible. However, performance can be improved by adding custom 849 ``__copy__`` and ``__deepcopy__`` methods. 850 851 For simple classes (deep)copy can be enabled by using the copy constructor, 852 which should look as follows: 853 854 .. code-block:: cpp 855 856 py::class_<Copyable>(m, "Copyable") 857 .def("__copy__", [](const Copyable &self) { 858 return Copyable(self); 859 }) 860 .def("__deepcopy__", [](const Copyable &self, py::dict) { 861 return Copyable(self); 862 }, "memo"_a); 863 864 .. note:: 865 866 Dynamic attributes will not be copied in this example. 867 868 .. [#f5] https://docs.python.org/3/library/copy.html 869 870 Multiple Inheritance 871 ==================== 872 873 pybind11 can create bindings for types that derive from multiple base types 874 (aka. *multiple inheritance*). To do so, specify all bases in the template 875 arguments of the ``class_`` declaration: 876 877 .. code-block:: cpp 878 879 py::class_<MyType, BaseType1, BaseType2, BaseType3>(m, "MyType") 880 ... 881 882 The base types can be specified in arbitrary order, and they can even be 883 interspersed with alias types and holder types (discussed earlier in this 884 document)---pybind11 will automatically find out which is which. The only 885 requirement is that the first template argument is the type to be declared. 886 887 It is also permitted to inherit multiply from exported C++ classes in Python, 888 as well as inheriting from multiple Python and/or pybind11-exported classes. 889 890 There is one caveat regarding the implementation of this feature: 891 892 When only one base type is specified for a C++ type that actually has multiple 893 bases, pybind11 will assume that it does not participate in multiple 894 inheritance, which can lead to undefined behavior. In such cases, add the tag 895 ``multiple_inheritance`` to the class constructor: 896 897 .. code-block:: cpp 898 899 py::class_<MyType, BaseType2>(m, "MyType", py::multiple_inheritance()); 900 901 The tag is redundant and does not need to be specified when multiple base types 902 are listed. 903 904 .. _module_local: 905 906 Module-local class bindings 907 =========================== 908 909 When creating a binding for a class, pybind11 by default makes that binding 910 "global" across modules. What this means is that a type defined in one module 911 can be returned from any module resulting in the same Python type. For 912 example, this allows the following: 913 914 .. code-block:: cpp 915 916 // In the module1.cpp binding code for module1: 917 py::class_<Pet>(m, "Pet") 918 .def(py::init<std::string>()) 919 .def_readonly("name", &Pet::name); 920 921 .. code-block:: cpp 922 923 // In the module2.cpp binding code for module2: 924 m.def("create_pet", [](std::string name) { return new Pet(name); }); 925 926 .. code-block:: pycon 927 928 >>> from module1 import Pet 929 >>> from module2 import create_pet 930 >>> pet1 = Pet("Kitty") 931 >>> pet2 = create_pet("Doggy") 932 >>> pet2.name() 933 'Doggy' 934 935 When writing binding code for a library, this is usually desirable: this 936 allows, for example, splitting up a complex library into multiple Python 937 modules. 938 939 In some cases, however, this can cause conflicts. For example, suppose two 940 unrelated modules make use of an external C++ library and each provide custom 941 bindings for one of that library's classes. This will result in an error when 942 a Python program attempts to import both modules (directly or indirectly) 943 because of conflicting definitions on the external type: 944 945 .. code-block:: cpp 946 947 // dogs.cpp 948 949 // Binding for external library class: 950 py::class<pets::Pet>(m, "Pet") 951 .def("name", &pets::Pet::name); 952 953 // Binding for local extension class: 954 py::class<Dog, pets::Pet>(m, "Dog") 955 .def(py::init<std::string>()); 956 957 .. code-block:: cpp 958 959 // cats.cpp, in a completely separate project from the above dogs.cpp. 960 961 // Binding for external library class: 962 py::class<pets::Pet>(m, "Pet") 963 .def("get_name", &pets::Pet::name); 964 965 // Binding for local extending class: 966 py::class<Cat, pets::Pet>(m, "Cat") 967 .def(py::init<std::string>()); 968 969 .. code-block:: pycon 970 971 >>> import cats 972 >>> import dogs 973 Traceback (most recent call last): 974 File "<stdin>", line 1, in <module> 975 ImportError: generic_type: type "Pet" is already registered! 976 977 To get around this, you can tell pybind11 to keep the external class binding 978 localized to the module by passing the ``py::module_local()`` attribute into 979 the ``py::class_`` constructor: 980 981 .. code-block:: cpp 982 983 // Pet binding in dogs.cpp: 984 py::class<pets::Pet>(m, "Pet", py::module_local()) 985 .def("name", &pets::Pet::name); 986 987 .. code-block:: cpp 988 989 // Pet binding in cats.cpp: 990 py::class<pets::Pet>(m, "Pet", py::module_local()) 991 .def("get_name", &pets::Pet::name); 992 993 This makes the Python-side ``dogs.Pet`` and ``cats.Pet`` into distinct classes, 994 avoiding the conflict and allowing both modules to be loaded. C++ code in the 995 ``dogs`` module that casts or returns a ``Pet`` instance will result in a 996 ``dogs.Pet`` Python instance, while C++ code in the ``cats`` module will result 997 in a ``cats.Pet`` Python instance. 998 999 This does come with two caveats, however: First, external modules cannot return 1000 or cast a ``Pet`` instance to Python (unless they also provide their own local 1001 bindings). Second, from the Python point of view they are two distinct classes. 1002 1003 Note that the locality only applies in the C++ -> Python direction. When 1004 passing such a ``py::module_local`` type into a C++ function, the module-local 1005 classes are still considered. This means that if the following function is 1006 added to any module (including but not limited to the ``cats`` and ``dogs`` 1007 modules above) it will be callable with either a ``dogs.Pet`` or ``cats.Pet`` 1008 argument: 1009 1010 .. code-block:: cpp 1011 1012 m.def("pet_name", [](const pets::Pet &pet) { return pet.name(); }); 1013 1014 For example, suppose the above function is added to each of ``cats.cpp``, 1015 ``dogs.cpp`` and ``frogs.cpp`` (where ``frogs.cpp`` is some other module that 1016 does *not* bind ``Pets`` at all). 1017 1018 .. code-block:: pycon 1019 1020 >>> import cats, dogs, frogs # No error because of the added py::module_local() 1021 >>> mycat, mydog = cats.Cat("Fluffy"), dogs.Dog("Rover") 1022 >>> (cats.pet_name(mycat), dogs.pet_name(mydog)) 1023 ('Fluffy', 'Rover') 1024 >>> (cats.pet_name(mydog), dogs.pet_name(mycat), frogs.pet_name(mycat)) 1025 ('Rover', 'Fluffy', 'Fluffy') 1026 1027 It is possible to use ``py::module_local()`` registrations in one module even 1028 if another module registers the same type globally: within the module with the 1029 module-local definition, all C++ instances will be cast to the associated bound 1030 Python type. In other modules any such values are converted to the global 1031 Python type created elsewhere. 1032 1033 .. note:: 1034 1035 STL bindings (as provided via the optional :file:`pybind11/stl_bind.h` 1036 header) apply ``py::module_local`` by default when the bound type might 1037 conflict with other modules; see :ref:`stl_bind` for details. 1038 1039 .. note:: 1040 1041 The localization of the bound types is actually tied to the shared object 1042 or binary generated by the compiler/linker. For typical modules created 1043 with ``PYBIND11_MODULE()``, this distinction is not significant. It is 1044 possible, however, when :ref:`embedding` to embed multiple modules in the 1045 same binary (see :ref:`embedding_modules`). In such a case, the 1046 localization will apply across all embedded modules within the same binary. 1047 1048 .. seealso:: 1049 1050 The file :file:`tests/test_local_bindings.cpp` contains additional examples 1051 that demonstrate how ``py::module_local()`` works. 1052 1053 Binding protected member functions 1054 ================================== 1055 1056 It's normally not possible to expose ``protected`` member functions to Python: 1057 1058 .. code-block:: cpp 1059 1060 class A { 1061 protected: 1062 int foo() const { return 42; } 1063 }; 1064 1065 py::class_<A>(m, "A") 1066 .def("foo", &A::foo); // error: 'foo' is a protected member of 'A' 1067 1068 On one hand, this is good because non-``public`` members aren't meant to be 1069 accessed from the outside. But we may want to make use of ``protected`` 1070 functions in derived Python classes. 1071 1072 The following pattern makes this possible: 1073 1074 .. code-block:: cpp 1075 1076 class A { 1077 protected: 1078 int foo() const { return 42; } 1079 }; 1080 1081 class Publicist : public A { // helper type for exposing protected functions 1082 public: 1083 using A::foo; // inherited with different access modifier 1084 }; 1085 1086 py::class_<A>(m, "A") // bind the primary class 1087 .def("foo", &Publicist::foo); // expose protected methods via the publicist 1088 1089 This works because ``&Publicist::foo`` is exactly the same function as 1090 ``&A::foo`` (same signature and address), just with a different access 1091 modifier. The only purpose of the ``Publicist`` helper class is to make 1092 the function name ``public``. 1093 1094 If the intent is to expose ``protected`` ``virtual`` functions which can be 1095 overridden in Python, the publicist pattern can be combined with the previously 1096 described trampoline: 1097 1098 .. code-block:: cpp 1099 1100 class A { 1101 public: 1102 virtual ~A() = default; 1103 1104 protected: 1105 virtual int foo() const { return 42; } 1106 }; 1107 1108 class Trampoline : public A { 1109 public: 1110 int foo() const override { PYBIND11_OVERRIDE(int, A, foo, ); } 1111 }; 1112 1113 class Publicist : public A { 1114 public: 1115 using A::foo; 1116 }; 1117 1118 py::class_<A, Trampoline>(m, "A") // <-- `Trampoline` here 1119 .def("foo", &Publicist::foo); // <-- `Publicist` here, not `Trampoline`! 1120 1121 Binding final classes 1122 ===================== 1123 1124 Some classes may not be appropriate to inherit from. In C++11, classes can 1125 use the ``final`` specifier to ensure that a class cannot be inherited from. 1126 The ``py::is_final`` attribute can be used to ensure that Python classes 1127 cannot inherit from a specified type. The underlying C++ type does not need 1128 to be declared final. 1129 1130 .. code-block:: cpp 1131 1132 class IsFinal final {}; 1133 1134 py::class_<IsFinal>(m, "IsFinal", py::is_final()); 1135 1136 When you try to inherit from such a class in Python, you will now get this 1137 error: 1138 1139 .. code-block:: pycon 1140 1141 >>> class PyFinalChild(IsFinal): 1142 ... pass 1143 ... 1144 TypeError: type 'IsFinal' is not an acceptable base type 1145 1146 .. note:: This attribute is currently ignored on PyPy 1147 1148 .. versionadded:: 2.6 1149 1150 Binding classes with template parameters 1151 ======================================== 1152 1153 pybind11 can also wrap classes that have template parameters. Consider these classes: 1154 1155 .. code-block:: cpp 1156 1157 struct Cat {}; 1158 struct Dog {}; 1159 1160 template <typename PetType> 1161 struct Cage { 1162 Cage(PetType& pet); 1163 PetType& get(); 1164 }; 1165 1166 C++ templates may only be instantiated at compile time, so pybind11 can only 1167 wrap instantiated templated classes. You cannot wrap a non-instantiated template: 1168 1169 .. code-block:: cpp 1170 1171 // BROKEN (this will not compile) 1172 py::class_<Cage>(m, "Cage"); 1173 .def("get", &Cage::get); 1174 1175 You must explicitly specify each template/type combination that you want to 1176 wrap separately. 1177 1178 .. code-block:: cpp 1179 1180 // ok 1181 py::class_<Cage<Cat>>(m, "CatCage") 1182 .def("get", &Cage<Cat>::get); 1183 1184 // ok 1185 py::class_<Cage<Dog>>(m, "DogCage") 1186 .def("get", &Cage<Dog>::get); 1187 1188 If your class methods have template parameters you can wrap those as well, 1189 but once again each instantiation must be explicitly specified: 1190 1191 .. code-block:: cpp 1192 1193 typename <typename T> 1194 struct MyClass { 1195 template <typename V> 1196 T fn(V v); 1197 }; 1198 1199 py::class<MyClass<int>>(m, "MyClassT") 1200 .def("fn", &MyClass<int>::fn<std::string>); 1201 1202 Custom automatic downcasters 1203 ============================ 1204 1205 As explained in :ref:`inheritance`, pybind11 comes with built-in 1206 understanding of the dynamic type of polymorphic objects in C++; that 1207 is, returning a Pet to Python produces a Python object that knows it's 1208 wrapping a Dog, if Pet has virtual methods and pybind11 knows about 1209 Dog and this Pet is in fact a Dog. Sometimes, you might want to 1210 provide this automatic downcasting behavior when creating bindings for 1211 a class hierarchy that does not use standard C++ polymorphism, such as 1212 LLVM [#f4]_. As long as there's some way to determine at runtime 1213 whether a downcast is safe, you can proceed by specializing the 1214 ``pybind11::polymorphic_type_hook`` template: 1215 1216 .. code-block:: cpp 1217 1218 enum class PetKind { Cat, Dog, Zebra }; 1219 struct Pet { // Not polymorphic: has no virtual methods 1220 const PetKind kind; 1221 int age = 0; 1222 protected: 1223 Pet(PetKind _kind) : kind(_kind) {} 1224 }; 1225 struct Dog : Pet { 1226 Dog() : Pet(PetKind::Dog) {} 1227 std::string sound = "woof!"; 1228 std::string bark() const { return sound; } 1229 }; 1230 1231 namespace PYBIND11_NAMESPACE { 1232 template<> struct polymorphic_type_hook<Pet> { 1233 static const void *get(const Pet *src, const std::type_info*& type) { 1234 // note that src may be nullptr 1235 if (src && src->kind == PetKind::Dog) { 1236 type = &typeid(Dog); 1237 return static_cast<const Dog*>(src); 1238 } 1239 return src; 1240 } 1241 }; 1242 } // namespace PYBIND11_NAMESPACE 1243 1244 When pybind11 wants to convert a C++ pointer of type ``Base*`` to a 1245 Python object, it calls ``polymorphic_type_hook<Base>::get()`` to 1246 determine if a downcast is possible. The ``get()`` function should use 1247 whatever runtime information is available to determine if its ``src`` 1248 parameter is in fact an instance of some class ``Derived`` that 1249 inherits from ``Base``. If it finds such a ``Derived``, it sets ``type 1250 = &typeid(Derived)`` and returns a pointer to the ``Derived`` object 1251 that contains ``src``. Otherwise, it just returns ``src``, leaving 1252 ``type`` at its default value of nullptr. If you set ``type`` to a 1253 type that pybind11 doesn't know about, no downcasting will occur, and 1254 the original ``src`` pointer will be used with its static type 1255 ``Base*``. 1256 1257 It is critical that the returned pointer and ``type`` argument of 1258 ``get()`` agree with each other: if ``type`` is set to something 1259 non-null, the returned pointer must point to the start of an object 1260 whose type is ``type``. If the hierarchy being exposed uses only 1261 single inheritance, a simple ``return src;`` will achieve this just 1262 fine, but in the general case, you must cast ``src`` to the 1263 appropriate derived-class pointer (e.g. using 1264 ``static_cast<Derived>(src)``) before allowing it to be returned as a 1265 ``void*``. 1266 1267 .. [#f4] https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html 1268 1269 .. note:: 1270 1271 pybind11's standard support for downcasting objects whose types 1272 have virtual methods is implemented using 1273 ``polymorphic_type_hook`` too, using the standard C++ ability to 1274 determine the most-derived type of a polymorphic object using 1275 ``typeid()`` and to cast a base pointer to that most-derived type 1276 (even if you don't know what it is) using ``dynamic_cast<void*>``. 1277 1278 .. seealso:: 1279 1280 The file :file:`tests/test_tagbased_polymorphic.cpp` contains a 1281 more complete example, including a demonstration of how to provide 1282 automatic downcasting for an entire class hierarchy without 1283 writing one get() function for each class. 1284 1285 Accessing the type object 1286 ========================= 1287 1288 You can get the type object from a C++ class that has already been registered using: 1289 1290 .. code-block:: cpp 1291 1292 py::type T_py = py::type::of<T>(); 1293 1294 You can directly use ``py::type::of(ob)`` to get the type object from any python 1295 object, just like ``type(ob)`` in Python. 1296 1297 .. note:: 1298 1299 Other types, like ``py::type::of<int>()``, do not work, see :ref:`type-conversions`. 1300 1301 .. versionadded:: 2.6 1302 1303 Custom type setup 1304 ================= 1305 1306 For advanced use cases, such as enabling garbage collection support, you may 1307 wish to directly manipulate the ``PyHeapTypeObject`` corresponding to a 1308 ``py::class_`` definition. 1309 1310 You can do that using ``py::custom_type_setup``: 1311 1312 .. code-block:: cpp 1313 1314 struct OwnsPythonObjects { 1315 py::object value = py::none(); 1316 }; 1317 py::class_<OwnsPythonObjects> cls( 1318 m, "OwnsPythonObjects", py::custom_type_setup([](PyHeapTypeObject *heap_type) { 1319 auto *type = &heap_type->ht_type; 1320 type->tp_flags |= Py_TPFLAGS_HAVE_GC; 1321 type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) { 1322 auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base)); 1323 Py_VISIT(self.value.ptr()); 1324 return 0; 1325 }; 1326 type->tp_clear = [](PyObject *self_base) { 1327 auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base)); 1328 self.value = py::none(); 1329 return 0; 1330 }; 1331 })); 1332 cls.def(py::init<>()); 1333 cls.def_readwrite("value", &OwnsPythonObjects::value); 1334 1335 .. versionadded:: 2.8