From: Ezio Melotti Date: Thu, 8 Nov 2012 09:06:01 +0000 (+0200) Subject: #11481: merge with 3.2. X-Git-Tag: v3.3.1rc1~668 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b77dc4ec5f5901f3de106de8a7369df2cf46940e;p=python #11481: merge with 3.2. --- b77dc4ec5f5901f3de106de8a7369df2cf46940e diff --cc Doc/library/copyreg.rst index 41061e515a,f3721d1929..50d5879ae5 --- a/Doc/library/copyreg.rst +++ b/Doc/library/copyreg.rst @@@ -32,8 -33,28 +33,30 @@@ Such constructors may be factory functi returned by *function* at pickling time. :exc:`TypeError` will be raised if *object* is a class or *constructor* is not callable. - See the :mod:`pickle` module for more details on the interface expected of - *function* and *constructor*. - + See the :mod:`pickle` module for more details on the interface + expected of *function* and *constructor*. Note that the + :attr:`~pickle.Pickler.dispatch_table` attribute of a pickler + object or subclass of :class:`pickle.Pickler` can also be used for + declaring reduction functions. + + Example + ------- + + The example below would like to show how to register a pickle function and how + it will be used: + + >>> import copyreg, 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,) + ... + >>> copyreg.pickle(C, pickle_c) + >>> c = C(1) + >>> d = copy.copy(c) + pickling a C instance... + >>> p = pickle.dumps(c) + pickling a C instance...