[Tutor] pickling and unpickling custom classes
kent37 at tds.net
Tue Oct 28 12:29:06 CET 2008
On Mon, Oct 27, 2008 at 11:40 PM, Lex Flagel <flagel at iastate.edu> wrote:
> I'm using a simple class called Hash, which I picked up from the following site:
> I like using this Hash object for its convenience, but it won't
> unpickle. Is there fix to the code example below (either the pickler
> or the Hash object) that will get it to unpickle (pickle.loads)?
You have to override Hash.__reduce__(), the values returned by
defaultdict.__reduce__() are not correct.
This works. The result of calling __reduce__() is a tuple, hence the
awkward code to build a new value:
r = defaultdict.__reduce__(self)
# override __init__ args
return (r, (), r, r, r)
More information about the Tutor