Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b77dc4e

Browse files
committed
#11481: merge with 3.2.
2 parents 60e22b9 + 78b18d4 commit b77dc4e

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

Doc/library/copy.rst

Lines changed: 2 additions & 2 deletions
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

Lines changed: 26 additions & 3 deletions
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

@@ -37,3 +38,25 @@ Such constructors may be factory functions or class instances.
3738
:attr:`~pickle.Pickler.dispatch_table` attribute of a pickler
3839
object or subclass of :class:`pickle.Pickler` can also be used for
3940
declaring reduction functions.
41+
42+
Example
43+
-------
44+
45+
The example below would like to show how to register a pickle function and how
46+
it will be used:
47+
48+
>>> import copyreg, copy, pickle
49+
>>> class C(object):
50+
... def __init__(self, a):
51+
... self.a = a
52+
...
53+
>>> def pickle_c(c):
54+
... print("pickling a C instance...")
55+
... return C, (c.a,)
56+
...
57+
>>> copyreg.pickle(C, pickle_c)
58+
>>> c = C(1)
59+
>>> d = copy.copy(c)
60+
pickling a C instance...
61+
>>> p = pickle.dumps(c)
62+
pickling a C instance...

0 commit comments

Comments
 (0)