github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/docs/advanced/functions.rst (about) 1 Functions 2 ######### 3 4 Before proceeding with this section, make sure that you are already familiar 5 with the basics of binding functions and classes, as explained in :doc:`/basics` 6 and :doc:`/classes`. The following guide is applicable to both free and member 7 functions, i.e. *methods* in Python. 8 9 .. _return_value_policies: 10 11 Return value policies 12 ===================== 13 14 Python and C++ use fundamentally different ways of managing the memory and 15 lifetime of objects managed by them. This can lead to issues when creating 16 bindings for functions that return a non-trivial type. Just by looking at the 17 type information, it is not clear whether Python should take charge of the 18 returned value and eventually free its resources, or if this is handled on the 19 C++ side. For this reason, pybind11 provides a several *return value policy* 20 annotations that can be passed to the :func:`module_::def` and 21 :func:`class_::def` functions. The default policy is 22 :enum:`return_value_policy::automatic`. 23 24 Return value policies are tricky, and it's very important to get them right. 25 Just to illustrate what can go wrong, consider the following simple example: 26 27 .. code-block:: cpp 28 29 /* Function declaration */ 30 Data *get_data() { return _data; /* (pointer to a static data structure) */ } 31 ... 32 33 /* Binding code */ 34 m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python 35 36 What's going on here? When ``get_data()`` is called from Python, the return 37 value (a native C++ type) must be wrapped to turn it into a usable Python type. 38 In this case, the default return value policy (:enum:`return_value_policy::automatic`) 39 causes pybind11 to assume ownership of the static ``_data`` instance. 40 41 When Python's garbage collector eventually deletes the Python 42 wrapper, pybind11 will also attempt to delete the C++ instance (via ``operator 43 delete()``) due to the implied ownership. At this point, the entire application 44 will come crashing down, though errors could also be more subtle and involve 45 silent data corruption. 46 47 In the above example, the policy :enum:`return_value_policy::reference` should have 48 been specified so that the global data instance is only *referenced* without any 49 implied transfer of ownership, i.e.: 50 51 .. code-block:: cpp 52 53 m.def("get_data", &get_data, py::return_value_policy::reference); 54 55 On the other hand, this is not the right policy for many other situations, 56 where ignoring ownership could lead to resource leaks. 57 As a developer using pybind11, it's important to be familiar with the different 58 return value policies, including which situation calls for which one of them. 59 The following table provides an overview of available policies: 60 61 .. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}| 62 63 +--------------------------------------------------+----------------------------------------------------------------------------+ 64 | Return value policy | Description | 65 +==================================================+============================================================================+ 66 | :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take | 67 | | ownership. Python will call the destructor and delete operator when the | 68 | | object's reference count reaches zero. Undefined behavior ensues when the | 69 | | C++ side does the same, or when the data was not dynamically allocated. | 70 +--------------------------------------------------+----------------------------------------------------------------------------+ 71 | :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. | 72 | | This policy is comparably safe because the lifetimes of the two instances | 73 | | are decoupled. | 74 +--------------------------------------------------+----------------------------------------------------------------------------+ 75 | :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance | 76 | | that will be owned by Python. This policy is comparably safe because the | 77 | | lifetimes of the two instances (move source and destination) are decoupled.| 78 +--------------------------------------------------+----------------------------------------------------------------------------+ 79 | :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is | 80 | | responsible for managing the object's lifetime and deallocating it when | 81 | | it is no longer used. Warning: undefined behavior will ensue when the C++ | 82 | | side deletes an object that is still referenced and used by Python. | 83 +--------------------------------------------------+----------------------------------------------------------------------------+ 84 | :enum:`return_value_policy::reference_internal` | Indicates that the lifetime of the return value is tied to the lifetime | 85 | | of a parent object, namely the implicit ``this``, or ``self`` argument of | 86 | | the called method or property. Internally, this policy works just like | 87 | | :enum:`return_value_policy::reference` but additionally applies a | 88 | | ``keep_alive<0, 1>`` *call policy* (described in the next section) that | 89 | | prevents the parent object from being garbage collected as long as the | 90 | | return value is referenced by Python. This is the default policy for | 91 | | property getters created via ``def_property``, ``def_readwrite``, etc. | 92 +--------------------------------------------------+----------------------------------------------------------------------------+ 93 | :enum:`return_value_policy::automatic` | This policy falls back to the policy | 94 | | :enum:`return_value_policy::take_ownership` when the return value is a | 95 | | pointer. Otherwise, it uses :enum:`return_value_policy::move` or | 96 | | :enum:`return_value_policy::copy` for rvalue and lvalue references, | 97 | | respectively. See above for a description of what all of these different | 98 | | policies do. This is the default policy for ``py::class_``-wrapped types. | 99 +--------------------------------------------------+----------------------------------------------------------------------------+ 100 | :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the | 101 | | return value is a pointer. This is the default conversion policy for | 102 | | function arguments when calling Python functions manually from C++ code | 103 | | (i.e. via ``handle::operator()``) and the casters in ``pybind11/stl.h``. | 104 | | You probably won't need to use this explicitly. | 105 +--------------------------------------------------+----------------------------------------------------------------------------+ 106 107 Return value policies can also be applied to properties: 108 109 .. code-block:: cpp 110 111 class_<MyClass>(m, "MyClass") 112 .def_property("data", &MyClass::getData, &MyClass::setData, 113 py::return_value_policy::copy); 114 115 Technically, the code above applies the policy to both the getter and the 116 setter function, however, the setter doesn't really care about *return* 117 value policies which makes this a convenient terse syntax. Alternatively, 118 targeted arguments can be passed through the :class:`cpp_function` constructor: 119 120 .. code-block:: cpp 121 122 class_<MyClass>(m, "MyClass") 123 .def_property("data", 124 py::cpp_function(&MyClass::getData, py::return_value_policy::copy), 125 py::cpp_function(&MyClass::setData) 126 ); 127 128 .. warning:: 129 130 Code with invalid return value policies might access uninitialized memory or 131 free data structures multiple times, which can lead to hard-to-debug 132 non-determinism and segmentation faults, hence it is worth spending the 133 time to understand all the different options in the table above. 134 135 .. note:: 136 137 One important aspect of the above policies is that they only apply to 138 instances which pybind11 has *not* seen before, in which case the policy 139 clarifies essential questions about the return value's lifetime and 140 ownership. When pybind11 knows the instance already (as identified by its 141 type and address in memory), it will return the existing Python object 142 wrapper rather than creating a new copy. 143 144 .. note:: 145 146 The next section on :ref:`call_policies` discusses *call policies* that can be 147 specified *in addition* to a return value policy from the list above. Call 148 policies indicate reference relationships that can involve both return values 149 and parameters of functions. 150 151 .. note:: 152 153 As an alternative to elaborate call policies and lifetime management logic, 154 consider using smart pointers (see the section on :ref:`smart_pointers` for 155 details). Smart pointers can tell whether an object is still referenced from 156 C++ or Python, which generally eliminates the kinds of inconsistencies that 157 can lead to crashes or undefined behavior. For functions returning smart 158 pointers, it is not necessary to specify a return value policy. 159 160 .. _call_policies: 161 162 Additional call policies 163 ======================== 164 165 In addition to the above return value policies, further *call policies* can be 166 specified to indicate dependencies between parameters or ensure a certain state 167 for the function call. 168 169 Keep alive 170 ---------- 171 172 In general, this policy is required when the C++ object is any kind of container 173 and another object is being added to the container. ``keep_alive<Nurse, Patient>`` 174 indicates that the argument with index ``Patient`` should be kept alive at least 175 until the argument with index ``Nurse`` is freed by the garbage collector. Argument 176 indices start at one, while zero refers to the return value. For methods, index 177 ``1`` refers to the implicit ``this`` pointer, while regular arguments begin at 178 index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse`` 179 with value ``None`` is detected at runtime, the call policy does nothing. 180 181 When the nurse is not a pybind11-registered type, the implementation internally 182 relies on the ability to create a *weak reference* to the nurse object. When 183 the nurse object is not a pybind11-registered type and does not support weak 184 references, an exception will be thrown. 185 186 If you use an incorrect argument index, you will get a ``RuntimeError`` saying 187 ``Could not activate keep_alive!``. You should review the indices you're using. 188 189 Consider the following example: here, the binding code for a list append 190 operation ties the lifetime of the newly added element to the underlying 191 container: 192 193 .. code-block:: cpp 194 195 py::class_<List>(m, "List") 196 .def("append", &List::append, py::keep_alive<1, 2>()); 197 198 For consistency, the argument indexing is identical for constructors. Index 199 ``1`` still refers to the implicit ``this`` pointer, i.e. the object which is 200 being constructed. Index ``0`` refers to the return type which is presumed to 201 be ``void`` when a constructor is viewed like a function. The following example 202 ties the lifetime of the constructor element to the constructed object: 203 204 .. code-block:: cpp 205 206 py::class_<Nurse>(m, "Nurse") 207 .def(py::init<Patient &>(), py::keep_alive<1, 2>()); 208 209 .. note:: 210 211 ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse, 212 Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient == 213 0) policies from Boost.Python. 214 215 Call guard 216 ---------- 217 218 The ``call_guard<T>`` policy allows any scope guard type ``T`` to be placed 219 around the function call. For example, this definition: 220 221 .. code-block:: cpp 222 223 m.def("foo", foo, py::call_guard<T>()); 224 225 is equivalent to the following pseudocode: 226 227 .. code-block:: cpp 228 229 m.def("foo", [](args...) { 230 T scope_guard; 231 return foo(args...); // forwarded arguments 232 }); 233 234 The only requirement is that ``T`` is default-constructible, but otherwise any 235 scope guard will work. This is very useful in combination with ``gil_scoped_release``. 236 See :ref:`gil`. 237 238 Multiple guards can also be specified as ``py::call_guard<T1, T2, T3...>``. The 239 constructor order is left to right and destruction happens in reverse. 240 241 .. seealso:: 242 243 The file :file:`tests/test_call_policies.cpp` contains a complete example 244 that demonstrates using `keep_alive` and `call_guard` in more detail. 245 246 .. _python_objects_as_args: 247 248 Python objects as arguments 249 =========================== 250 251 pybind11 exposes all major Python types using thin C++ wrapper classes. These 252 wrapper classes can also be used as parameters of functions in bindings, which 253 makes it possible to directly work with native Python types on the C++ side. 254 For instance, the following statement iterates over a Python ``dict``: 255 256 .. code-block:: cpp 257 258 void print_dict(const py::dict& dict) { 259 /* Easily interact with Python types */ 260 for (auto item : dict) 261 std::cout << "key=" << std::string(py::str(item.first)) << ", " 262 << "value=" << std::string(py::str(item.second)) << std::endl; 263 } 264 265 It can be exported: 266 267 .. code-block:: cpp 268 269 m.def("print_dict", &print_dict); 270 271 And used in Python as usual: 272 273 .. code-block:: pycon 274 275 >>> print_dict({"foo": 123, "bar": "hello"}) 276 key=foo, value=123 277 key=bar, value=hello 278 279 For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`. 280 281 Accepting \*args and \*\*kwargs 282 =============================== 283 284 Python provides a useful mechanism to define functions that accept arbitrary 285 numbers of arguments and keyword arguments: 286 287 .. code-block:: python 288 289 def generic(*args, **kwargs): 290 ... # do something with args and kwargs 291 292 Such functions can also be created using pybind11: 293 294 .. code-block:: cpp 295 296 void generic(py::args args, const py::kwargs& kwargs) { 297 /// .. do something with args 298 if (kwargs) 299 /// .. do something with kwargs 300 } 301 302 /// Binding code 303 m.def("generic", &generic); 304 305 The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives 306 from ``py::dict``. 307 308 You may also use just one or the other, and may combine these with other 309 arguments. Note, however, that ``py::kwargs`` must always be the last argument 310 of the function, and ``py::args`` implies that any further arguments are 311 keyword-only (see :ref:`keyword_only_arguments`). 312 313 Please refer to the other examples for details on how to iterate over these, 314 and on how to cast their entries into C++ objects. A demonstration is also 315 available in ``tests/test_kwargs_and_defaults.cpp``. 316 317 .. note:: 318 319 When combining \*args or \*\*kwargs with :ref:`keyword_args` you should 320 *not* include ``py::arg`` tags for the ``py::args`` and ``py::kwargs`` 321 arguments. 322 323 Default arguments revisited 324 =========================== 325 326 The section on :ref:`default_args` previously discussed basic usage of default 327 arguments using pybind11. One noteworthy aspect of their implementation is that 328 default arguments are converted to Python objects right at declaration time. 329 Consider the following example: 330 331 .. code-block:: cpp 332 333 py::class_<MyClass>("MyClass") 334 .def("myFunction", py::arg("arg") = SomeType(123)); 335 336 In this case, pybind11 must already be set up to deal with values of the type 337 ``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an 338 exception will be thrown. 339 340 Another aspect worth highlighting is that the "preview" of the default argument 341 in the function signature is generated using the object's ``__repr__`` method. 342 If not available, the signature may not be very helpful, e.g.: 343 344 .. code-block:: pycon 345 346 FUNCTIONS 347 ... 348 | myFunction(...) 349 | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType 350 ... 351 352 The first way of addressing this is by defining ``SomeType.__repr__``. 353 Alternatively, it is possible to specify the human-readable preview of the 354 default argument manually using the ``arg_v`` notation: 355 356 .. code-block:: cpp 357 358 py::class_<MyClass>("MyClass") 359 .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)")); 360 361 Sometimes it may be necessary to pass a null pointer value as a default 362 argument. In this case, remember to cast it to the underlying type in question, 363 like so: 364 365 .. code-block:: cpp 366 367 py::class_<MyClass>("MyClass") 368 .def("myFunction", py::arg("arg") = static_cast<SomeType *>(nullptr)); 369 370 .. _keyword_only_arguments: 371 372 Keyword-only arguments 373 ====================== 374 375 Python implements keyword-only arguments by specifying an unnamed ``*`` 376 argument in a function definition: 377 378 .. code-block:: python 379 380 def f(a, *, b): # a can be positional or via keyword; b must be via keyword 381 pass 382 383 384 f(a=1, b=2) # good 385 f(b=2, a=1) # good 386 f(1, b=2) # good 387 f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given 388 389 Pybind11 provides a ``py::kw_only`` object that allows you to implement 390 the same behaviour by specifying the object between positional and keyword-only 391 argument annotations when registering the function: 392 393 .. code-block:: cpp 394 395 m.def("f", [](int a, int b) { /* ... */ }, 396 py::arg("a"), py::kw_only(), py::arg("b")); 397 398 .. versionadded:: 2.6 399 400 A ``py::args`` argument implies that any following arguments are keyword-only, 401 as if ``py::kw_only()`` had been specified in the same relative location of the 402 argument list as the ``py::args`` argument. The ``py::kw_only()`` may be 403 included to be explicit about this, but is not required. 404 405 .. versionchanged:: 2.9 406 This can now be combined with ``py::args``. Before, ``py::args`` could only 407 occur at the end of the argument list, or immediately before a ``py::kwargs`` 408 argument at the end. 409 410 411 Positional-only arguments 412 ========================= 413 414 Python 3.8 introduced a new positional-only argument syntax, using ``/`` in the 415 function definition (note that this has been a convention for CPython 416 positional arguments, such as in ``pow()``, since Python 2). You can 417 do the same thing in any version of Python using ``py::pos_only()``: 418 419 .. code-block:: cpp 420 421 m.def("f", [](int a, int b) { /* ... */ }, 422 py::arg("a"), py::pos_only(), py::arg("b")); 423 424 You now cannot give argument ``a`` by keyword. This can be combined with 425 keyword-only arguments, as well. 426 427 .. versionadded:: 2.6 428 429 .. _nonconverting_arguments: 430 431 Non-converting arguments 432 ======================== 433 434 Certain argument types may support conversion from one type to another. Some 435 examples of conversions are: 436 437 * :ref:`implicit_conversions` declared using ``py::implicitly_convertible<A,B>()`` 438 * Calling a method accepting a double with an integer argument 439 * Calling a ``std::complex<float>`` argument with a non-complex python type 440 (for example, with a float). (Requires the optional ``pybind11/complex.h`` 441 header). 442 * Calling a function taking an Eigen matrix reference with a numpy array of the 443 wrong type or of an incompatible data layout. (Requires the optional 444 ``pybind11/eigen.h`` header). 445 446 This behaviour is sometimes undesirable: the binding code may prefer to raise 447 an error rather than convert the argument. This behaviour can be obtained 448 through ``py::arg`` by calling the ``.noconvert()`` method of the ``py::arg`` 449 object, such as: 450 451 .. code-block:: cpp 452 453 m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert()); 454 m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f")); 455 456 Attempting the call the second function (the one without ``.noconvert()``) with 457 an integer will succeed, but attempting to call the ``.noconvert()`` version 458 will fail with a ``TypeError``: 459 460 .. code-block:: pycon 461 462 >>> floats_preferred(4) 463 2.0 464 >>> floats_only(4) 465 Traceback (most recent call last): 466 File "<stdin>", line 1, in <module> 467 TypeError: floats_only(): incompatible function arguments. The following argument types are supported: 468 1. (f: float) -> float 469 470 Invoked with: 4 471 472 You may, of course, combine this with the :var:`_a` shorthand notation (see 473 :ref:`keyword_args`) and/or :ref:`default_args`. It is also permitted to omit 474 the argument name by using the ``py::arg()`` constructor without an argument 475 name, i.e. by specifying ``py::arg().noconvert()``. 476 477 .. note:: 478 479 When specifying ``py::arg`` options it is necessary to provide the same 480 number of options as the bound function has arguments. Thus if you want to 481 enable no-convert behaviour for just one of several arguments, you will 482 need to specify a ``py::arg()`` annotation for each argument with the 483 no-convert argument modified to ``py::arg().noconvert()``. 484 485 .. _none_arguments: 486 487 Allow/Prohibiting None arguments 488 ================================ 489 490 When a C++ type registered with :class:`py::class_` is passed as an argument to 491 a function taking the instance as pointer or shared holder (e.g. ``shared_ptr`` 492 or a custom, copyable holder as described in :ref:`smart_pointers`), pybind 493 allows ``None`` to be passed from Python which results in calling the C++ 494 function with ``nullptr`` (or an empty holder) for the argument. 495 496 To explicitly enable or disable this behaviour, using the 497 ``.none`` method of the :class:`py::arg` object: 498 499 .. code-block:: cpp 500 501 py::class_<Dog>(m, "Dog").def(py::init<>()); 502 py::class_<Cat>(m, "Cat").def(py::init<>()); 503 m.def("bark", [](Dog *dog) -> std::string { 504 if (dog) return "woof!"; /* Called with a Dog instance */ 505 else return "(no dog)"; /* Called with None, dog == nullptr */ 506 }, py::arg("dog").none(true)); 507 m.def("meow", [](Cat *cat) -> std::string { 508 // Can't be called with None argument 509 return "meow"; 510 }, py::arg("cat").none(false)); 511 512 With the above, the Python call ``bark(None)`` will return the string ``"(no 513 dog)"``, while attempting to call ``meow(None)`` will raise a ``TypeError``: 514 515 .. code-block:: pycon 516 517 >>> from animals import Dog, Cat, bark, meow 518 >>> bark(Dog()) 519 'woof!' 520 >>> meow(Cat()) 521 'meow' 522 >>> bark(None) 523 '(no dog)' 524 >>> meow(None) 525 Traceback (most recent call last): 526 File "<stdin>", line 1, in <module> 527 TypeError: meow(): incompatible function arguments. The following argument types are supported: 528 1. (cat: animals.Cat) -> str 529 530 Invoked with: None 531 532 The default behaviour when the tag is unspecified is to allow ``None``. 533 534 .. note:: 535 536 Even when ``.none(true)`` is specified for an argument, ``None`` will be converted to a 537 ``nullptr`` *only* for custom and :ref:`opaque <opaque>` types. Pointers to built-in types 538 (``double *``, ``int *``, ...) and STL types (``std::vector<T> *``, ...; if ``pybind11/stl.h`` 539 is included) are copied when converted to C++ (see :doc:`/advanced/cast/overview`) and will 540 not allow ``None`` as argument. To pass optional argument of these copied types consider 541 using ``std::optional<T>`` 542 543 .. _overload_resolution: 544 545 Overload resolution order 546 ========================= 547 548 When a function or method with multiple overloads is called from Python, 549 pybind11 determines which overload to call in two passes. The first pass 550 attempts to call each overload without allowing argument conversion (as if 551 every argument had been specified as ``py::arg().noconvert()`` as described 552 above). 553 554 If no overload succeeds in the no-conversion first pass, a second pass is 555 attempted in which argument conversion is allowed (except where prohibited via 556 an explicit ``py::arg().noconvert()`` attribute in the function definition). 557 558 If the second pass also fails a ``TypeError`` is raised. 559 560 Within each pass, overloads are tried in the order they were registered with 561 pybind11. If the ``py::prepend()`` tag is added to the definition, a function 562 can be placed at the beginning of the overload sequence instead, allowing user 563 overloads to proceed built in functions. 564 565 What this means in practice is that pybind11 will prefer any overload that does 566 not require conversion of arguments to an overload that does, but otherwise 567 prefers earlier-defined overloads to later-defined ones. 568 569 .. note:: 570 571 pybind11 does *not* further prioritize based on the number/pattern of 572 overloaded arguments. That is, pybind11 does not prioritize a function 573 requiring one conversion over one requiring three, but only prioritizes 574 overloads requiring no conversion at all to overloads that require 575 conversion of at least one argument. 576 577 .. versionadded:: 2.6 578 579 The ``py::prepend()`` tag. 580 581 Binding functions with template parameters 582 ========================================== 583 584 You can bind functions that have template parameters. Here's a function: 585 586 .. code-block:: cpp 587 588 template <typename T> 589 void set(T t); 590 591 C++ templates cannot be instantiated at runtime, so you cannot bind the 592 non-instantiated function: 593 594 .. code-block:: cpp 595 596 // BROKEN (this will not compile) 597 m.def("set", &set); 598 599 You must bind each instantiated function template separately. You may bind 600 each instantiation with the same name, which will be treated the same as 601 an overloaded function: 602 603 .. code-block:: cpp 604 605 m.def("set", &set<int>); 606 m.def("set", &set<std::string>); 607 608 Sometimes it's more clear to bind them with separate names, which is also 609 an option: 610 611 .. code-block:: cpp 612 613 m.def("setInt", &set<int>); 614 m.def("setString", &set<std::string>);