By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

pickling class instances with __slots__

P: n/a
I would greatly appreciate an advice on the following matter that was
very much discussed circa 2002 but in all this discussion I could not
find the final answer with a code example. Neither I can find it in
Python documentation.
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
there a universally accepted code for each method? If so, what is it?
If there is no standard, what works?
TIA
Alex

Oct 29 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Alex <Ou****@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

Oct 29 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.