object.rst (11986:c12e4625ab56) | object.rst (12037:d28054ac6ec9) |
---|---|
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 | 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 |
|
36Calling Python functions 37======================== 38 39It is also possible to call python functions via ``operator()``. 40 41.. code-block:: cpp 42 43 py::function f = <...>; --- 8 unchanged lines hidden (view full) --- 52 ... # function code 53 54 f(1234, say="hello", to=some_instance) # keyword call in Python 55 56In C++, the same call can be made using: 57 58.. code-block:: cpp 59 | 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 |
60 using pybind11::literals; // to bring in the `_a` literal | 62 using namespace pybind11::literals; // to bring in the `_a` literal |
61 f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++ 62 63Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with 64other arguments: 65 66.. code-block:: cpp 67 68 // * unpacking --- 28 unchanged lines hidden --- | 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 --- |