You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

  1. Python types
  2. ############
  3. Available wrappers
  4. ==================
  5. All major Python types are available as thin C++ wrapper classes. These
  6. can also be used as function parameters -- see :ref:`python_objects_as_args`.
  7. Available types include :class:`handle`, :class:`object`, :class:`bool_`,
  8. :class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`,
  9. :class:`list`, :class:`dict`, :class:`slice`, :class:`none`, :class:`capsule`,
  10. :class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
  11. :class:`array`, and :class:`array_t`.
  12. Casting back and forth
  13. ======================
  14. In this kind of mixed code, it is often necessary to convert arbitrary C++
  15. types to Python, which can be done using :func:`py::cast`:
  16. .. code-block:: cpp
  17. MyClass *cls = ..;
  18. py::object obj = py::cast(cls);
  19. The reverse direction uses the following syntax:
  20. .. code-block:: cpp
  21. py::object obj = ...;
  22. MyClass *cls = obj.cast<MyClass *>();
  23. When conversion fails, both directions throw the exception :class:`cast_error`.
  24. Calling Python functions
  25. ========================
  26. It is also possible to call python functions via ``operator()``.
  27. .. code-block:: cpp
  28. py::function f = <...>;
  29. py::object result_py = f(1234, "hello", some_instance);
  30. MyClass &result = result_py.cast<MyClass>();
  31. Keyword arguments are also supported. In Python, there is the usual call syntax:
  32. .. code-block:: python
  33. def f(number, say, to):
  34. ... # function code
  35. f(1234, say="hello", to=some_instance) # keyword call in Python
  36. In C++, the same call can be made using:
  37. .. code-block:: cpp
  38. using pybind11::literals; // to bring in the `_a` literal
  39. f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
  40. Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
  41. other arguments:
  42. .. code-block:: cpp
  43. // * unpacking
  44. py::tuple args = py::make_tuple(1234, "hello", some_instance);
  45. f(*args);
  46. // ** unpacking
  47. py::dict kwargs = py::dict("number"_a=1234, "say"_a="hello", "to"_a=some_instance);
  48. f(**kwargs);
  49. // mixed keywords, * and ** unpacking
  50. py::tuple args = py::make_tuple(1234);
  51. py::dict kwargs = py::dict("to"_a=some_instance);
  52. f(*args, "say"_a="hello", **kwargs);
  53. Generalized unpacking according to PEP448_ is also supported:
  54. .. code-block:: cpp
  55. py::dict kwargs1 = py::dict("number"_a=1234);
  56. py::dict kwargs2 = py::dict("to"_a=some_instance);
  57. f(**kwargs1, "say"_a="hello", **kwargs2);
  58. .. seealso::
  59. The file :file:`tests/test_python_types.cpp` contains a complete
  60. example that demonstrates passing native Python types in more detail. The
  61. file :file:`tests/test_callbacks.cpp` presents a few examples of calling
  62. Python functions from C++, including keywords arguments and unpacking.
  63. .. _PEP448: https://www.python.org/dev/peps/pep-0448/