1Python types 2############ 3 4Available wrappers 5================== 6 7All major Python types are available as thin C++ wrapper classes. These 8can also be used as function parameters -- see :ref:`python_objects_as_args`. --- 19 unchanged lines hidden (view full) --- 28 29.. code-block:: cpp 30 31 py::object obj = ...; 32 MyClass *cls = obj.cast<MyClass *>(); 33 34When conversion fails, both directions throw the exception :class:`cast_error`. 35 |
36.. _calling_python_functions: 37 |
38Calling Python functions 39======================== 40 41It is also possible to call python functions via ``operator()``. 42 43.. code-block:: cpp 44 45 py::function f = <...>; --- 8 unchanged lines hidden (view full) --- 54 ... # function code 55 56 f(1234, say="hello", to=some_instance) # keyword call in Python 57 58In C++, the same call can be made using: 59 60.. code-block:: cpp 61 |
62 using namespace pybind11::literals; // to bring in the `_a` literal |
63 f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++ 64 65Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with 66other arguments: 67 68.. code-block:: cpp 69 70 // * unpacking --- 28 unchanged lines hidden --- |