@@ -9,17 +9,60 @@ Available wrappers
9
9
All major Python types are available as thin C++ wrapper classes. These
10
10
can also be used as function parameters -- see :ref: `python_objects_as_args `.
11
11
12
- Available types include :class: `handle `, :class: `object `, :class: `bool_ `,
13
- :class: `int_ `, :class: `float_ `, :class: `str `, :class: `bytes `, :class: `tuple `,
14
- :class: `list `, :class: `dict `, :class: `slice `, :class: `none `, :class: `capsule `,
15
- :class: `iterable `, :class: `iterator `, :class: `function `, :class: `buffer `,
16
- :class: `array `, and :class: `array_t `.
12
+ Available types include :class: `handle `, :class: `object `, :class: `namespace_ `,
13
+ :class: `bool_ `, :class: `int_ `, :class: `float_ `, :class: `str `, :class: `bytes `,
14
+ :class: `tuple `, :class: `list `, :class: `dict `, :class: `slice `, :class: `none `,
15
+ :class: `capsule `, :class: `iterable `, :class: `iterator `, :class: `function `,
16
+ :class: `buffer `, :class: ` array `, and :class: `array_t `.
17
17
18
18
.. warning ::
19
19
20
20
Be sure to review the :ref: `pytypes_gotchas ` before using this heavily in
21
21
your C++ API.
22
22
23
+ .. _instantiating_compound_types :
24
+
25
+ Instantiating compound Python types from C++
26
+ ============================================
27
+
28
+ A tuple of python objects can be instantiated using :func: `py::make_tuple `:
29
+
30
+ .. code-block :: cpp
31
+
32
+ py::tuple tup = py::make_tuple(42, py::none(), "spam");
33
+
34
+ Each element is converted to a supported Python type.
35
+
36
+ Dictionaries can be initialized in the :class: `dict ` constructor:
37
+
38
+ .. code-block :: cpp
39
+
40
+ using namespace pybind11::literals; // to bring in the `_a` literal
41
+ py::dict d("spam"_a=py::none(), "eggs"_a=42);
42
+
43
+ A `simple namespace `_ can be instantiated in the :class: `namespace_ `
44
+ constructor:
45
+
46
+ .. code-block :: cpp
47
+
48
+ using namespace pybind11::literals; // to bring in the `_a` literal
49
+ py::namespace_ ns("spam"_a=py::none(), "eggs"_a=42);
50
+
51
+ The keys in the arguments to the :class: `namespace_ ` constructor become the
52
+ attribute names in the resulting namespace. Attributes on a namespace can be
53
+ modified with the :func: `py::delattr `, :func: `py::getattr `, and
54
+ :func: `py::setattr ` functions. Namespaces can be useful as stand-ins for class
55
+ instances.
56
+
57
+ .. note ::
58
+
59
+ The ``namespace_ `` type is not available in Python 2.
60
+
61
+ .. versionchanged :: 2.7
62
+ ``namespace_ `` added.
63
+
64
+ .. _simple namespace : https://docs.python.org/3/library/types.html#types.SimpleNamespace
65
+
23
66
.. _casting_back_and_forth :
24
67
25
68
Casting back and forth
@@ -30,7 +73,7 @@ types to Python, which can be done using :func:`py::cast`:
30
73
31
74
.. code-block :: cpp
32
75
33
- MyClass *cls = ..;
76
+ MyClass *cls = ... ;
34
77
py::object obj = py::cast(cls);
35
78
36
79
The reverse direction uses the following syntax:
0 commit comments