Skip to content

Commit 78b18d4

Browse files
committed
#11481: update copyreg docs and add example.
1 parent 44dbd07 commit 78b18d4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

Doc/library/copy.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ of lists by assigning a slice of the entire list, for example,
6767

6868
Classes can use the same interfaces to control copying that they use to control
6969
pickling. See the description of module :mod:`pickle` for information on these
70-
methods. The :mod:`copy` module does not use the :mod:`copyreg` registration
71-
module.
70+
methods. In fact, :mod:`copy` module uses the registered pickle functions from
71+
:mod:`copyreg` module.
7272

7373
.. index::
7474
single: __copy__() (copy protocol)

Doc/library/copyreg.rst

+26-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
module: pickle
1010
module: copy
1111

12-
The :mod:`copyreg` module provides support for the :mod:`pickle` module. The
13-
:mod:`copy` module is likely to use this in the future as well. It provides
14-
configuration information about object constructors which are not classes.
12+
The :mod:`copyreg` module offers a way to define fuctions used while pickling
13+
specific objects. The :mod:`pickle` and :mod:`copy` modules use those functions
14+
when pickling/copying those objects. The module provides configuration
15+
information about object constructors which are not classes.
1516
Such constructors may be factory functions or class instances.
1617

1718

@@ -35,3 +36,25 @@ Such constructors may be factory functions or class instances.
3536
See the :mod:`pickle` module for more details on the interface expected of
3637
*function* and *constructor*.
3738

39+
40+
Example
41+
-------
42+
43+
The example below would like to show how to register a pickle function and how
44+
it will be used:
45+
46+
>>> import copyreg, copy, pickle
47+
>>> class C(object):
48+
... def __init__(self, a):
49+
... self.a = a
50+
...
51+
>>> def pickle_c(c):
52+
... print("pickling a C instance...")
53+
... return C, (c.a,)
54+
...
55+
>>> copyreg.pickle(C, pickle_c)
56+
>>> c = C(1)
57+
>>> d = copy.copy(c)
58+
pickling a C instance...
59+
>>> p = pickle.dumps(c)
60+
pickling a C instance...

0 commit comments

Comments
 (0)