pickling class instances with __slots__
Alex Martelli
aleaxit at yahoo.com
Sat Oct 29 00:30:12 EDT 2005
Alex <OurLab at gmail.com> wrote:
...
> I have a series of new classes with child-parent relationship and each
> has unique __slots__. They don't have __dict__ . I need to be able to
> pickle and unpickle them. As far as I could understand, I need to
> provide __getstate__ and __setstate__ methods for each class. Is
Right.
> there a universally accepted code for each method? If so, what is it?
> If there is no standard, what works?
Lots of things work, the simplest is something like:
>>> class wehaveslots(object):
... __slots__ = 'a', 'b', 'c'
... def __getstate__(self): return self.a, self.b, self.c
... def __setstate__(self, tup): self.a, self.b, self.c = tup
(plus presumably other methods, but those don't matter for pickle).
Alex
More information about the Python-list
mailing list