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.

98 lines
2.9 KiB

8 years ago
8 years ago
  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. Calling Python functions
  26. ========================
  27. It is also possible to call python functions via ``operator()``.
  28. .. code-block:: cpp
  29. py::function f = <...>;
  30. py::object result_py = f(1234, "hello", some_instance);
  31. MyClass &result = result_py.cast<MyClass>();
  32. Keyword arguments are also supported. In Python, there is the usual call syntax:
  33. .. code-block:: python
  34. def f(number, say, to):
  35. ... # function code
  36. f(1234, say="hello", to=some_instance) # keyword call in Python
  37. In C++, the same call can be made using:
  38. .. code-block:: cpp
  39. using namespace pybind11::literals; // to bring in the `_a` literal
  40. f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
  41. Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
  42. other arguments:
  43. .. code-block:: cpp
  44. // * unpacking
  45. py::tuple args = py::make_tuple(1234, "hello", some_instance);
  46. f(*args);
  47. // ** unpacking
  48. py::dict kwargs = py::dict("number"_a=1234, "say"_a="hello", "to"_a=some_instance);
  49. f(**kwargs);
  50. // mixed keywords, * and ** unpacking
  51. py::tuple args = py::make_tuple(1234);
  52. py::dict kwargs = py::dict("to"_a=some_instance);
  53. f(*args, "say"_a="hello", **kwargs);
  54. Generalized unpacking according to PEP448_ is also supported:
  55. .. code-block:: cpp
  56. py::dict kwargs1 = py::dict("number"_a=1234);
  57. py::dict kwargs2 = py::dict("to"_a=some_instance);
  58. f(**kwargs1, "say"_a="hello", **kwargs2);
  59. .. seealso::
  60. The file :file:`tests/test_python_types.cpp` contains a complete
  61. example that demonstrates passing native Python types in more detail. The
  62. file :file:`tests/test_callbacks.cpp` presents a few examples of calling
  63. Python functions from C++, including keywords arguments and unpacking.
  64. .. _PEP448: https://www.python.org/dev/peps/pep-0448/