functions.rst revision 11986
111986Sandreas.sandberg@arm.comFunctions
211986Sandreas.sandberg@arm.com#########
311986Sandreas.sandberg@arm.com
411986Sandreas.sandberg@arm.comBefore proceeding with this section, make sure that you are already familiar
511986Sandreas.sandberg@arm.comwith the basics of binding functions and classes, as explained in :doc:`/basics`
611986Sandreas.sandberg@arm.comand :doc:`/classes`. The following guide is applicable to both free and member
711986Sandreas.sandberg@arm.comfunctions, i.e. *methods* in Python.
811986Sandreas.sandberg@arm.com
911986Sandreas.sandberg@arm.comReturn value policies
1011986Sandreas.sandberg@arm.com=====================
1111986Sandreas.sandberg@arm.com
1211986Sandreas.sandberg@arm.comPython and C++ use fundamentally different ways of managing the memory and
1311986Sandreas.sandberg@arm.comlifetime of objects managed by them. This can lead to issues when creating
1411986Sandreas.sandberg@arm.combindings for functions that return a non-trivial type. Just by looking at the
1511986Sandreas.sandberg@arm.comtype information, it is not clear whether Python should take charge of the
1611986Sandreas.sandberg@arm.comreturned value and eventually free its resources, or if this is handled on the
1711986Sandreas.sandberg@arm.comC++ side. For this reason, pybind11 provides a several `return value policy`
1811986Sandreas.sandberg@arm.comannotations that can be passed to the :func:`module::def` and
1911986Sandreas.sandberg@arm.com:func:`class_::def` functions. The default policy is
2011986Sandreas.sandberg@arm.com:enum:`return_value_policy::automatic`.
2111986Sandreas.sandberg@arm.com
2211986Sandreas.sandberg@arm.comReturn value policies are tricky, and it's very important to get them right.
2311986Sandreas.sandberg@arm.comJust to illustrate what can go wrong, consider the following simple example:
2411986Sandreas.sandberg@arm.com
2511986Sandreas.sandberg@arm.com.. code-block:: cpp
2611986Sandreas.sandberg@arm.com
2711986Sandreas.sandberg@arm.com    /* Function declaration */ 
2811986Sandreas.sandberg@arm.com    Data *get_data() { return _data; /* (pointer to a static data structure) */ }
2911986Sandreas.sandberg@arm.com    ...
3011986Sandreas.sandberg@arm.com
3111986Sandreas.sandberg@arm.com    /* Binding code */ 
3211986Sandreas.sandberg@arm.com    m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python
3311986Sandreas.sandberg@arm.com
3411986Sandreas.sandberg@arm.comWhat's going on here? When ``get_data()`` is called from Python, the return
3511986Sandreas.sandberg@arm.comvalue (a native C++ type) must be wrapped to turn it into a usable Python type.
3611986Sandreas.sandberg@arm.comIn this case, the default return value policy (:enum:`return_value_policy::automatic`)
3711986Sandreas.sandberg@arm.comcauses pybind11 to assume ownership of the static ``_data`` instance.
3811986Sandreas.sandberg@arm.com
3911986Sandreas.sandberg@arm.comWhen Python's garbage collector eventually deletes the Python
4011986Sandreas.sandberg@arm.comwrapper, pybind11 will also attempt to delete the C++ instance (via ``operator
4111986Sandreas.sandberg@arm.comdelete()``) due to the implied ownership. At this point, the entire application
4211986Sandreas.sandberg@arm.comwill come crashing down, though errors could also be more subtle and involve
4311986Sandreas.sandberg@arm.comsilent data corruption.
4411986Sandreas.sandberg@arm.com
4511986Sandreas.sandberg@arm.comIn the above example, the policy :enum:`return_value_policy::reference` should have
4611986Sandreas.sandberg@arm.combeen specified so that the global data instance is only *referenced* without any
4711986Sandreas.sandberg@arm.comimplied transfer of ownership, i.e.: 
4811986Sandreas.sandberg@arm.com
4911986Sandreas.sandberg@arm.com.. code-block:: cpp
5011986Sandreas.sandberg@arm.com
5111986Sandreas.sandberg@arm.com    m.def("get_data", &get_data, return_value_policy::reference);
5211986Sandreas.sandberg@arm.com
5311986Sandreas.sandberg@arm.comOn the other hand, this is not the right policy for many other situations,
5411986Sandreas.sandberg@arm.comwhere ignoring ownership could lead to resource leaks.
5511986Sandreas.sandberg@arm.comAs a developer using pybind11, it's important to be familiar with the different
5611986Sandreas.sandberg@arm.comreturn value policies, including which situation calls for which one of them.
5711986Sandreas.sandberg@arm.comThe following table provides an overview of available policies:
5811986Sandreas.sandberg@arm.com
5911986Sandreas.sandberg@arm.com.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
6011986Sandreas.sandberg@arm.com
6111986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
6211986Sandreas.sandberg@arm.com| Return value policy                              | Description                                                                |
6311986Sandreas.sandberg@arm.com+==================================================+============================================================================+
6411986Sandreas.sandberg@arm.com| :enum:`return_value_policy::take_ownership`      | Reference an existing object (i.e. do not create a new copy) and take      |
6511986Sandreas.sandberg@arm.com|                                                  | ownership. Python will call the destructor and delete operator when the    |
6611986Sandreas.sandberg@arm.com|                                                  | object's reference count reaches zero. Undefined behavior ensues when the  |
6711986Sandreas.sandberg@arm.com|                                                  | C++ side does the same, or when the data was not dynamically allocated.    |
6811986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
6911986Sandreas.sandberg@arm.com| :enum:`return_value_policy::copy`                | Create a new copy of the returned object, which will be owned by Python.   |
7011986Sandreas.sandberg@arm.com|                                                  | This policy is comparably safe because the lifetimes of the two instances  |
7111986Sandreas.sandberg@arm.com|                                                  | are decoupled.                                                             |
7211986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
7311986Sandreas.sandberg@arm.com| :enum:`return_value_policy::move`                | Use ``std::move`` to move the return value contents into a new instance    |
7411986Sandreas.sandberg@arm.com|                                                  | that will be owned by Python. This policy is comparably safe because the   |
7511986Sandreas.sandberg@arm.com|                                                  | lifetimes of the two instances (move source and destination) are decoupled.|
7611986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
7711986Sandreas.sandberg@arm.com| :enum:`return_value_policy::reference`           | Reference an existing object, but do not take ownership. The C++ side is   |
7811986Sandreas.sandberg@arm.com|                                                  | responsible for managing the object's lifetime and deallocating it when    |
7911986Sandreas.sandberg@arm.com|                                                  | it is no longer used. Warning: undefined behavior will ensue when the C++  |
8011986Sandreas.sandberg@arm.com|                                                  | side deletes an object that is still referenced and used by Python.        |
8111986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
8211986Sandreas.sandberg@arm.com| :enum:`return_value_policy::reference_internal`  | Indicates that the lifetime of the return value is tied to the lifetime    |
8311986Sandreas.sandberg@arm.com|                                                  | of a parent object, namely the implicit ``this``, or ``self`` argument of  |
8411986Sandreas.sandberg@arm.com|                                                  | the called method or property. Internally, this policy works just like     |
8511986Sandreas.sandberg@arm.com|                                                  | :enum:`return_value_policy::reference` but additionally applies a          |
8611986Sandreas.sandberg@arm.com|                                                  | ``keep_alive<0, 1>`` *call policy* (described in the next section) that    |
8711986Sandreas.sandberg@arm.com|                                                  | prevents the parent object from being garbage collected as long as the     |
8811986Sandreas.sandberg@arm.com|                                                  | return value is referenced by Python. This is the default policy for       |
8911986Sandreas.sandberg@arm.com|                                                  | property getters created via ``def_property``, ``def_readwrite``, etc.     |
9011986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
9111986Sandreas.sandberg@arm.com| :enum:`return_value_policy::automatic`           | This is the default return value policy, which falls back to the policy    |
9211986Sandreas.sandberg@arm.com|                                                  | :enum:`return_value_policy::take_ownership` when the return value is a     |
9311986Sandreas.sandberg@arm.com|                                                  | pointer. Otherwise, it uses :enum:`return_value::move` or                  |
9411986Sandreas.sandberg@arm.com|                                                  | :enum:`return_value::copy` for rvalue and lvalue references, respectively. |
9511986Sandreas.sandberg@arm.com|                                                  | See above for a description of what all of these different policies do.    |
9611986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
9711986Sandreas.sandberg@arm.com| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the   |
9811986Sandreas.sandberg@arm.com|                                                  | return value is a pointer. This is the default conversion policy for       |
9911986Sandreas.sandberg@arm.com|                                                  | function arguments when calling Python functions manually from C++ code    |
10011986Sandreas.sandberg@arm.com|                                                  | (i.e. via handle::operator()). You probably won't need to use this.        |
10111986Sandreas.sandberg@arm.com+--------------------------------------------------+----------------------------------------------------------------------------+
10211986Sandreas.sandberg@arm.com
10311986Sandreas.sandberg@arm.comReturn value policies can also be applied to properties:
10411986Sandreas.sandberg@arm.com
10511986Sandreas.sandberg@arm.com.. code-block:: cpp
10611986Sandreas.sandberg@arm.com
10711986Sandreas.sandberg@arm.com    class_<MyClass>(m, "MyClass")
10811986Sandreas.sandberg@arm.com        .def_property("data", &MyClass::getData, &MyClass::setData,
10911986Sandreas.sandberg@arm.com                      py::return_value_policy::copy);
11011986Sandreas.sandberg@arm.com
11111986Sandreas.sandberg@arm.comTechnically, the code above applies the policy to both the getter and the
11211986Sandreas.sandberg@arm.comsetter function, however, the setter doesn't really care about *return*
11311986Sandreas.sandberg@arm.comvalue policies which makes this a convenient terse syntax. Alternatively,
11411986Sandreas.sandberg@arm.comtargeted arguments can be passed through the :class:`cpp_function` constructor:
11511986Sandreas.sandberg@arm.com
11611986Sandreas.sandberg@arm.com.. code-block:: cpp
11711986Sandreas.sandberg@arm.com
11811986Sandreas.sandberg@arm.com    class_<MyClass>(m, "MyClass")
11911986Sandreas.sandberg@arm.com        .def_property("data"
12011986Sandreas.sandberg@arm.com            py::cpp_function(&MyClass::getData, py::return_value_policy::copy),
12111986Sandreas.sandberg@arm.com            py::cpp_function(&MyClass::setData)
12211986Sandreas.sandberg@arm.com        );
12311986Sandreas.sandberg@arm.com
12411986Sandreas.sandberg@arm.com.. warning::
12511986Sandreas.sandberg@arm.com
12611986Sandreas.sandberg@arm.com    Code with invalid return value policies might access unitialized memory or
12711986Sandreas.sandberg@arm.com    free data structures multiple times, which can lead to hard-to-debug
12811986Sandreas.sandberg@arm.com    non-determinism and segmentation faults, hence it is worth spending the
12911986Sandreas.sandberg@arm.com    time to understand all the different options in the table above.
13011986Sandreas.sandberg@arm.com
13111986Sandreas.sandberg@arm.com.. note::
13211986Sandreas.sandberg@arm.com
13311986Sandreas.sandberg@arm.com    One important aspect of the above policies is that they only apply to
13411986Sandreas.sandberg@arm.com    instances which pybind11 has *not* seen before, in which case the policy
13511986Sandreas.sandberg@arm.com    clarifies essential questions about the return value's lifetime and
13611986Sandreas.sandberg@arm.com    ownership.  When pybind11 knows the instance already (as identified by its
13711986Sandreas.sandberg@arm.com    type and address in memory), it will return the existing Python object
13811986Sandreas.sandberg@arm.com    wrapper rather than creating a new copy.
13911986Sandreas.sandberg@arm.com
14011986Sandreas.sandberg@arm.com.. note::
14111986Sandreas.sandberg@arm.com
14211986Sandreas.sandberg@arm.com    The next section on :ref:`call_policies` discusses *call policies* that can be
14311986Sandreas.sandberg@arm.com    specified *in addition* to a return value policy from the list above. Call
14411986Sandreas.sandberg@arm.com    policies indicate reference relationships that can involve both return values
14511986Sandreas.sandberg@arm.com    and parameters of functions.
14611986Sandreas.sandberg@arm.com
14711986Sandreas.sandberg@arm.com.. note::
14811986Sandreas.sandberg@arm.com
14911986Sandreas.sandberg@arm.com   As an alternative to elaborate call policies and lifetime management logic,
15011986Sandreas.sandberg@arm.com   consider using smart pointers (see the section on :ref:`smart_pointers` for
15111986Sandreas.sandberg@arm.com   details). Smart pointers can tell whether an object is still referenced from
15211986Sandreas.sandberg@arm.com   C++ or Python, which generally eliminates the kinds of inconsistencies that
15311986Sandreas.sandberg@arm.com   can lead to crashes or undefined behavior. For functions returning smart
15411986Sandreas.sandberg@arm.com   pointers, it is not necessary to specify a return value policy.
15511986Sandreas.sandberg@arm.com
15611986Sandreas.sandberg@arm.com.. _call_policies:
15711986Sandreas.sandberg@arm.com
15811986Sandreas.sandberg@arm.comAdditional call policies
15911986Sandreas.sandberg@arm.com========================
16011986Sandreas.sandberg@arm.com
16111986Sandreas.sandberg@arm.comIn addition to the above return value policies, further `call policies` can be
16211986Sandreas.sandberg@arm.comspecified to indicate dependencies between parameters. There is currently just
16311986Sandreas.sandberg@arm.comone policy named ``keep_alive<Nurse, Patient>``, which indicates that the
16411986Sandreas.sandberg@arm.comargument with index ``Patient`` should be kept alive at least until the
16511986Sandreas.sandberg@arm.comargument with index ``Nurse`` is freed by the garbage collector. Argument
16611986Sandreas.sandberg@arm.comindices start at one, while zero refers to the return value. For methods, index
16711986Sandreas.sandberg@arm.com``1`` refers to the implicit ``this`` pointer, while regular arguments begin at
16811986Sandreas.sandberg@arm.comindex ``2``. Arbitrarily many call policies can be specified. When a ``Nurse``
16911986Sandreas.sandberg@arm.comwith value ``None`` is detected at runtime, the call policy does nothing.
17011986Sandreas.sandberg@arm.com
17111986Sandreas.sandberg@arm.comThis feature internally relies on the ability to create a *weak reference* to
17211986Sandreas.sandberg@arm.comthe nurse object, which is permitted by all classes exposed via pybind11. When
17311986Sandreas.sandberg@arm.comthe nurse object does not support weak references, an exception will be thrown.
17411986Sandreas.sandberg@arm.com
17511986Sandreas.sandberg@arm.comConsider the following example: here, the binding code for a list append
17611986Sandreas.sandberg@arm.comoperation ties the lifetime of the newly added element to the underlying
17711986Sandreas.sandberg@arm.comcontainer:
17811986Sandreas.sandberg@arm.com
17911986Sandreas.sandberg@arm.com.. code-block:: cpp
18011986Sandreas.sandberg@arm.com
18111986Sandreas.sandberg@arm.com    py::class_<List>(m, "List")
18211986Sandreas.sandberg@arm.com        .def("append", &List::append, py::keep_alive<1, 2>());
18311986Sandreas.sandberg@arm.com
18411986Sandreas.sandberg@arm.com.. note::
18511986Sandreas.sandberg@arm.com
18611986Sandreas.sandberg@arm.com    ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
18711986Sandreas.sandberg@arm.com    Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
18811986Sandreas.sandberg@arm.com    0) policies from Boost.Python.
18911986Sandreas.sandberg@arm.com
19011986Sandreas.sandberg@arm.com.. seealso::
19111986Sandreas.sandberg@arm.com
19211986Sandreas.sandberg@arm.com    The file :file:`tests/test_keep_alive.cpp` contains a complete example
19311986Sandreas.sandberg@arm.com    that demonstrates using :class:`keep_alive` in more detail.
19411986Sandreas.sandberg@arm.com
19511986Sandreas.sandberg@arm.com.. _python_objects_as_args:
19611986Sandreas.sandberg@arm.com
19711986Sandreas.sandberg@arm.comPython objects as arguments
19811986Sandreas.sandberg@arm.com===========================
19911986Sandreas.sandberg@arm.com
20011986Sandreas.sandberg@arm.compybind11 exposes all major Python types using thin C++ wrapper classes. These
20111986Sandreas.sandberg@arm.comwrapper classes can also be used as parameters of functions in bindings, which
20211986Sandreas.sandberg@arm.commakes it possible to directly work with native Python types on the C++ side.
20311986Sandreas.sandberg@arm.comFor instance, the following statement iterates over a Python ``dict``:
20411986Sandreas.sandberg@arm.com
20511986Sandreas.sandberg@arm.com.. code-block:: cpp
20611986Sandreas.sandberg@arm.com
20711986Sandreas.sandberg@arm.com    void print_dict(py::dict dict) {
20811986Sandreas.sandberg@arm.com        /* Easily interact with Python types */
20911986Sandreas.sandberg@arm.com        for (auto item : dict)
21011986Sandreas.sandberg@arm.com            std::cout << "key=" << item.first << ", "
21111986Sandreas.sandberg@arm.com                      << "value=" << item.second << std::endl;
21211986Sandreas.sandberg@arm.com    }
21311986Sandreas.sandberg@arm.com
21411986Sandreas.sandberg@arm.comIt can be exported:
21511986Sandreas.sandberg@arm.com
21611986Sandreas.sandberg@arm.com.. code-block:: cpp
21711986Sandreas.sandberg@arm.com
21811986Sandreas.sandberg@arm.com    m.def("print_dict", &print_dict);
21911986Sandreas.sandberg@arm.com
22011986Sandreas.sandberg@arm.comAnd used in Python as usual:
22111986Sandreas.sandberg@arm.com
22211986Sandreas.sandberg@arm.com.. code-block:: pycon
22311986Sandreas.sandberg@arm.com
22411986Sandreas.sandberg@arm.com    >>> print_dict({'foo': 123, 'bar': 'hello'})
22511986Sandreas.sandberg@arm.com    key=foo, value=123
22611986Sandreas.sandberg@arm.com    key=bar, value=hello
22711986Sandreas.sandberg@arm.com
22811986Sandreas.sandberg@arm.comFor more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`.
22911986Sandreas.sandberg@arm.com
23011986Sandreas.sandberg@arm.comAccepting \*args and \*\*kwargs
23111986Sandreas.sandberg@arm.com===============================
23211986Sandreas.sandberg@arm.com
23311986Sandreas.sandberg@arm.comPython provides a useful mechanism to define functions that accept arbitrary
23411986Sandreas.sandberg@arm.comnumbers of arguments and keyword arguments:
23511986Sandreas.sandberg@arm.com
23611986Sandreas.sandberg@arm.com.. code-block:: python
23711986Sandreas.sandberg@arm.com
23811986Sandreas.sandberg@arm.com   def generic(*args, **kwargs):
23911986Sandreas.sandberg@arm.com       ...  # do something with args and kwargs
24011986Sandreas.sandberg@arm.com
24111986Sandreas.sandberg@arm.comSuch functions can also be created using pybind11:
24211986Sandreas.sandberg@arm.com
24311986Sandreas.sandberg@arm.com.. code-block:: cpp
24411986Sandreas.sandberg@arm.com
24511986Sandreas.sandberg@arm.com   void generic(py::args args, py::kwargs kwargs) {
24611986Sandreas.sandberg@arm.com       /// .. do something with args
24711986Sandreas.sandberg@arm.com       if (kwargs)
24811986Sandreas.sandberg@arm.com           /// .. do something with kwargs
24911986Sandreas.sandberg@arm.com   }
25011986Sandreas.sandberg@arm.com
25111986Sandreas.sandberg@arm.com   /// Binding code
25211986Sandreas.sandberg@arm.com   m.def("generic", &generic);
25311986Sandreas.sandberg@arm.com
25411986Sandreas.sandberg@arm.comThe class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives
25511986Sandreas.sandberg@arm.comfrom ``py::dict``. Note that the ``kwargs`` argument is invalid if no keyword
25611986Sandreas.sandberg@arm.comarguments were actually provided. Please refer to the other examples for
25711986Sandreas.sandberg@arm.comdetails on how to iterate over these, and on how to cast their entries into
25811986Sandreas.sandberg@arm.comC++ objects. A demonstration is also available in
25911986Sandreas.sandberg@arm.com``tests/test_kwargs_and_defaults.cpp``.
26011986Sandreas.sandberg@arm.com
26111986Sandreas.sandberg@arm.com.. warning::
26211986Sandreas.sandberg@arm.com
26311986Sandreas.sandberg@arm.com   Unlike Python, pybind11 does not allow combining normal parameters with the
26411986Sandreas.sandberg@arm.com   ``args`` / ``kwargs`` special parameters.
26511986Sandreas.sandberg@arm.com
26611986Sandreas.sandberg@arm.comDefault arguments revisited
26711986Sandreas.sandberg@arm.com===========================
26811986Sandreas.sandberg@arm.com
26911986Sandreas.sandberg@arm.comThe section on :ref:`default_args` previously discussed basic usage of default
27011986Sandreas.sandberg@arm.comarguments using pybind11. One noteworthy aspect of their implementation is that
27111986Sandreas.sandberg@arm.comdefault arguments are converted to Python objects right at declaration time.
27211986Sandreas.sandberg@arm.comConsider the following example:
27311986Sandreas.sandberg@arm.com
27411986Sandreas.sandberg@arm.com.. code-block:: cpp
27511986Sandreas.sandberg@arm.com
27611986Sandreas.sandberg@arm.com    py::class_<MyClass>("MyClass")
27711986Sandreas.sandberg@arm.com        .def("myFunction", py::arg("arg") = SomeType(123));
27811986Sandreas.sandberg@arm.com
27911986Sandreas.sandberg@arm.comIn this case, pybind11 must already be set up to deal with values of the type
28011986Sandreas.sandberg@arm.com``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
28111986Sandreas.sandberg@arm.comexception will be thrown.
28211986Sandreas.sandberg@arm.com
28311986Sandreas.sandberg@arm.comAnother aspect worth highlighting is that the "preview" of the default argument
28411986Sandreas.sandberg@arm.comin the function signature is generated using the object's ``__repr__`` method.
28511986Sandreas.sandberg@arm.comIf not available, the signature may not be very helpful, e.g.:
28611986Sandreas.sandberg@arm.com
28711986Sandreas.sandberg@arm.com.. code-block:: pycon
28811986Sandreas.sandberg@arm.com
28911986Sandreas.sandberg@arm.com    FUNCTIONS
29011986Sandreas.sandberg@arm.com    ...
29111986Sandreas.sandberg@arm.com    |  myFunction(...)
29211986Sandreas.sandberg@arm.com    |      Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
29311986Sandreas.sandberg@arm.com    ...
29411986Sandreas.sandberg@arm.com
29511986Sandreas.sandberg@arm.comThe first way of addressing this is by defining ``SomeType.__repr__``.
29611986Sandreas.sandberg@arm.comAlternatively, it is possible to specify the human-readable preview of the
29711986Sandreas.sandberg@arm.comdefault argument manually using the ``arg_v`` notation:
29811986Sandreas.sandberg@arm.com
29911986Sandreas.sandberg@arm.com.. code-block:: cpp
30011986Sandreas.sandberg@arm.com
30111986Sandreas.sandberg@arm.com    py::class_<MyClass>("MyClass")
30211986Sandreas.sandberg@arm.com        .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)"));
30311986Sandreas.sandberg@arm.com
30411986Sandreas.sandberg@arm.comSometimes it may be necessary to pass a null pointer value as a default
30511986Sandreas.sandberg@arm.comargument. In this case, remember to cast it to the underlying type in question,
30611986Sandreas.sandberg@arm.comlike so:
30711986Sandreas.sandberg@arm.com
30811986Sandreas.sandberg@arm.com.. code-block:: cpp
30911986Sandreas.sandberg@arm.com
31011986Sandreas.sandberg@arm.com    py::class_<MyClass>("MyClass")
31111986Sandreas.sandberg@arm.com        .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
312