Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
b5240089
Unverified
Commit
b5240089
authored
Jun 10, 2020
by
Matthijs van der Burgh
Committed by
GitHub
Jun 10, 2020
Browse files
Deepcopy documentation (#2242)
* (docs) convert note to real note * (docs) Add information about (deep)copy
parent
1e14930d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
7 deletions
+43
-7
docs/advanced/classes.rst
docs/advanced/classes.rst
+43
-7
No files found.
docs/advanced/classes.rst
View file @
b5240089
...
@@ -768,13 +768,17 @@ An instance can now be pickled as follows:
...
@@ -768,13 +768,17 @@ An instance can now be pickled as follows:
p.setExtra(15)
p.setExtra(15)
data = pickle.dumps(p, 2)
data = pickle.dumps(p, 2)
Note that only the cPickle module is supported on Python 2.7. The second
argument to ``dumps`` is also crucial: it selects the pickle protocol version
.. note::
2, since the older version 1 is not supported. Newer versions are also fine—for
Note that only the cPickle module is supported on Python 2.7.
instance, specify ``-1`` to always use the latest available version. Beware:
failure to follow these instructions will cause important pybind11 memory
The second argument to ``dumps`` is also crucial: it selects the pickle
allocation routines to be skipped during unpickling, which will likely lead to
protocol version 2, since the older version 1 is not supported. Newer
memory corruption and/or segmentation faults.
versions are also fine—for instance, specify ``-1`` to always use the
latest available version. Beware: failure to follow these instructions
will cause important pybind11 memory allocation routines to be skipped
during unpickling, which will likely lead to memory corruption and/or
segmentation faults.
.. seealso::
.. seealso::
...
@@ -784,6 +788,38 @@ memory corruption and/or segmentation faults.
...
@@ -784,6 +788,38 @@ memory corruption and/or segmentation faults.
.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
Deepcopy support
================
Python normally uses references in assignments. Sometimes a real copy is needed
to prevent changing all copies. The ``copy`` module [#f5]_ provides these
capabilities.
On Python 3, a class with pickle support is automatically also (deep)copy
compatible. However, performance can be improved by adding custom
``__copy__`` and ``__deepcopy__`` methods. With Python 2.7, these custom methods
are mandatory for (deep)copy compatibility, because pybind11 only supports
cPickle.
For simple classes (deep)copy can be enabled by using the copy constructor,
which should look as follows:
.. code-block:: cpp
py::class_<Copyable>(m, "Copyable")
.def("__copy__", [](const Copyable &self) {
return Copyable(self);
})
.def("__deepcopy__", [](const Copyable &self, py::dict) {
return Copyable(self);
}, "memo"_a);
.. note::
Dynamic attributes will not be copied in this example.
.. [#f5] https://docs.python.org/3/library/copy.html
Multiple Inheritance
Multiple Inheritance
====================
====================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment