stl.rst (11986:c12e4625ab56) | stl.rst (12037:d28054ac6ec9) |
---|---|
1STL containers 2############## 3 4Automatic conversion 5==================== 6 7When including the additional header file :file:`pybind11/stl.h`, conversions | 1STL containers 2############## 3 4Automatic conversion 5==================== 6 7When including the additional header file :file:`pybind11/stl.h`, conversions |
8between ``std::vector<>``, ``std::list<>``, ``std::set<>``, and ``std::map<>`` 9and the Python ``list``, ``set`` and ``dict`` data structures are automatically 10enabled. The types ``std::pair<>`` and ``std::tuple<>`` are already supported 11out of the box with just the core :file:`pybind11/pybind11.h` header. | 8between ``std::vector<>``/``std::list<>``/``std::array<>``, 9``std::set<>``/``std::unordered_set<>``, and 10``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and 11``dict`` data structures are automatically enabled. The types ``std::pair<>`` 12and ``std::tuple<>`` are already supported out of the box with just the core 13:file:`pybind11/pybind11.h` header. |
12 13The major downside of these implicit conversions is that containers must be 14converted (i.e. copied) on every Python->C++ and C++->Python transition, which 15can have implications on the program semantics and performance. Please read the 16next sections for more details and alternative approaches that avoid this. 17 18.. note:: 19 --- 47 unchanged lines hidden (view full) --- 67 68 class MyClass { 69 std::vector<int> contents; 70 }; 71 72 /* ... binding code ... */ 73 74 py::class_<MyClass>(m, "MyClass") | 14 15The major downside of these implicit conversions is that containers must be 16converted (i.e. copied) on every Python->C++ and C++->Python transition, which 17can have implications on the program semantics and performance. Please read the 18next sections for more details and alternative approaches that avoid this. 19 20.. note:: 21 --- 47 unchanged lines hidden (view full) --- 69 70 class MyClass { 71 std::vector<int> contents; 72 }; 73 74 /* ... binding code ... */ 75 76 py::class_<MyClass>(m, "MyClass") |
75 .def(py::init<>) | 77 .def(py::init<>()) |
76 .def_readwrite("contents", &MyClass::contents); 77 78In this case, properties can be read and written in their entirety. However, an 79``append`` operation involving such a list type has no effect: 80 81.. code-block:: pycon 82 83 >>> m = MyClass() --- 71 unchanged lines hidden --- | 78 .def_readwrite("contents", &MyClass::contents); 79 80In this case, properties can be read and written in their entirety. However, an 81``append`` operation involving such a list type has no effect: 82 83.. code-block:: pycon 84 85 >>> m = MyClass() --- 71 unchanged lines hidden --- |