471,122 Members | 881 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

pickling class instances with __slots__

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
1 1459
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.

Similar topics

8 posts views Thread by Hans Georg Krauthaeuser | last post: by
4 posts views Thread by fortepianissimo | last post: by
2 posts views Thread by Kirk Strauser | last post: by
reply views Thread by jeanphilippe.aumasson | last post: by
reply views Thread by Shahin Saadati | last post: by
6 posts views Thread by Alex Hunsley | last post: by
3 posts views Thread by Schüle Daniel | last post: by
14 posts views Thread by =?GB2312?B?zPC5zw==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.