1
:mod:`copyreg` --- Register :mod:`pickle` support functions
2
===========================================================
5
:synopsis: Register pickle support functions.
12
The :mod:`copyreg` module offers a way to define functions 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.
16
Such constructors may be factory functions or class instances.
19
.. function:: constructor(object)
21
Declares *object* to be a valid constructor. If *object* is not callable (and
22
hence not valid as a constructor), raises :exc:`TypeError`.
25
.. function:: pickle(type, function, constructor=None)
27
Declares that *function* should be used as a "reduction" function for objects
28
of type *type*. *function* should return either a string or a tuple
29
containing two or three elements.
31
The optional *constructor* parameter, if provided, is a callable object which
32
can be used to reconstruct the object when called with the tuple of arguments
33
returned by *function* at pickling time. :exc:`TypeError` will be raised if
34
*object* is a class or *constructor* is not callable.
36
See the :mod:`pickle` module for more details on the interface
37
expected of *function* and *constructor*. Note that the
38
:attr:`~pickle.Pickler.dispatch_table` attribute of a pickler
39
object or subclass of :class:`pickle.Pickler` can also be used for
40
declaring reduction functions.
45
The example below would like to show how to register a pickle function and how
48
>>> import copyreg, copy, pickle
50
... def __init__(self, a):
54
... print("pickling a C instance...")
57
>>> copyreg.pickle(C, pickle_c)
60
pickling a C instance...
61
>>> p = pickle.dumps(c)
62
pickling a C instance...