Question about unpickling dict subclass with custom __setstate__
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Sep 11 04:40:51 EDT 2009
En Thu, 10 Sep 2009 20:09:34 -0300, Matthew Wilson <matt at tplus1.com>
escribió:
> I subclassed the dict class and added a __setstate__ method because I
> want to add some extra steps when I unpickle these entities. This is a
> toy example of what I am doing:
>
> class Entity(dict):
>
> def __setstate__(self, d):
>
> log.debug("blah...")
>
> Based on my experiments, the data in d *IS NOT* the data stored in my
> instances when I do stuff like:
>
> e = Entity()
> e['a'] = 1
>
> Instead, the stuff in d is the data stored when I do stuff like:
>
> e.fibityfoo = 99
Yes. The dict contents are pickled directly; __getstate__ and __setstate__
are related to the instance attributes (fibityfoo), not its contents
(a->1).
Try defining __reduce__ or __reduce_ex__ instead.
> Is there anything I have to do to make sure that my real dictionary data
> is correctly reloaded during the unpickle phase? In other words, should
> I run super(Entity, self).__setstate__(d) or something like that?
That doesn't work, dict.__setstate__ doesn't even exist.
--
Gabriel Genellina
More information about the Python-list
mailing list