Skip to content

Commit efe8e75

Browse files
Explain how the pickle methods customize copying
1 parent e88e1c8 commit efe8e75

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

‎Doc/library/copy.rst‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ returns an instance of the same type.
152152
Customizing copying
153153
-------------------
154154

155-
.. index:: pair: module; pickle
156-
157-
Classes can use the same interfaces to control copying that they use to control
158-
pickling. See the description of module :mod:`pickle` for information on these
159-
methods. In fact, the :mod:`!copy` module uses the registered
160-
pickle functions from the :mod:`copyreg` module.
161-
162155
.. index::
163156
single: __copy__() (copy protocol)
164157
single: __deepcopy__() (copy protocol)
@@ -214,6 +207,23 @@ and a deep copy shares the ``library`` with the original:
214207
True
215208

216209

210+
.. index:: pair: module; pickle
211+
212+
A class which defines neither :meth:`~object.__copy__` nor
213+
:meth:`~object.__deepcopy__` is copied using the same interfaces that
214+
control pickling.
215+
The :mod:`!copy` module first looks for a function registered for the class
216+
with :func:`copyreg.pickle`, and otherwise calls the object's
217+
:meth:`~object.__reduce_ex__` method with protocol 4.
218+
The new object is then created from the returned value, and its state is
219+
restored with :meth:`~object.__setstate__` if the class defines it.
220+
A deep copy also deep copies the arguments and state in the returned value.
221+
This means that the :meth:`~object.__getnewargs_ex__`,
222+
:meth:`~object.__getstate__` and :meth:`~object.__setstate__` methods
223+
customize both pickling and copying.
224+
See the description of module :mod:`pickle` for information on these methods.
225+
226+
217227
.. index::
218228
single: __replace__() (replace protocol)
219229

0 commit comments

Comments
 (0)