#11481: update copy_reg docs and add example.

This commit is contained in:
Ezio Melotti 2012-11-08 10:59:03 +02:00
parent 26025d6385
commit b1c0e3b229
1 changed files with 26 additions and 5 deletions

View File

@ -14,11 +14,11 @@
module: cPickle module: cPickle
module: copy module: copy
The :mod:`copy_reg` module provides support for the :mod:`pickle` and The :mod:`copy_reg` module offers a way to define fuctions used while pickling
:mod:`cPickle` modules. The :mod:`copy` module is likely to use this in the specific objects. The :mod:`pickle`, :mod:`cPickle, and :mod:`copy` modules
future as well. It provides configuration information about object constructors use those functions when pickling/copying those objects. The module provides
which are not classes. Such constructors may be factory functions or class configuration information about object constructors which are not classes.
instances. Such constructors may be factory functions or class instances.
.. function:: constructor(object) .. function:: constructor(object)
@ -43,3 +43,24 @@ instances.
See the :mod:`pickle` module for more details on the interface expected of See the :mod:`pickle` module for more details on the interface expected of
*function* and *constructor*. *function* and *constructor*.
Example
-------
The example below would like to show how to register a pickle function and how
it will be used:
>>> import copy_reg, copy, pickle
>>> class C(object):
... def __init__(self, a):
... self.a = a
...
>>> def pickle_c(c):
... print("pickling a C instance...")
... return C, (c.a,)
...
>>> copy_reg.pickle(C, pickle_c)
>>> c = C(1)
>>> d = copy.copy(c)
pickling a C instance...
>>> p = pickle.dumps(c)
pickling a C instance...