[docs] pickle docs - what can be pickled

Eli Bendersky eliben at gmail.com
Tue Jan 1 16:37:07 CET 2013


On Tue, Jan 1, 2013 at 7:09 AM, Georg Brandl <georg at python.org> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 01/01/2013 01:28 AM, Eli Bendersky wrote:
> > In the pickle docs of 3.3, section 12.4.1 says:
> >
> > "instances of such classes whose __dict__ or __setstate__() is picklable"
> >
> > Does it actually mean __setstate__ or __getstate__ ?
>
> ISTM __setstate__ is correct, since that's the method that has to be
> looked up at unpickling time.  (__getstate__ is called at pickling time.)
>

Yes, __getstate__ is called at pickling time, which is precisely why it
should replace __setstate__ in the quoted statement, IMHO. Note that the
doc of __getstate__ says:

"Classes can further influence how their instances are pickled; if the
class defines the method
__getstate__()<http://docs.python.org/dev/library/pickle.html?highlight=pickle#pickle.object.__getstate__>,
it is called and the returned object is pickled as the contents for the
instance, instead of the contents of the instance’s dictionary. If the
__getstate__()<http://docs.python.org/dev/library/pickle.html?highlight=pickle#pickle.object.__getstate__>method
is absent, the instance’s
__dict__ <http://docs.python.org/dev/library/stdtypes.html#object.__dict__>is
pickled as usual."

Which further suggest that __getstate__ and __dict__ are, in a sense,
interchangeable.

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20130101/e75c03dc/attachment.html>


More information about the docs mailing list