How to pickle a subclass of tuple?

Greg Chapman glc at well.com
Fri Jun 4 18:09:25 CEST 2004


On Thu, 03 Jun 2004 19:26:37 +0300, Christos "TZOTZIOY" Georgiou
<tzot at sil-tec.gr> wrote:

>__getstate__ is easy:
>
>def __getstate__(self):
>    return tuple(self)
>
>but even
>
>def __getstate__(self):
>    return self
>
>seems to work, as far as Pickle.dump is concerned.  The problem is, how
>one writes a __setstate__ for an immutable class?

You'll probably need to use some of the extended pickle protocol hooks
documented here:

http://www.python.org/peps/pep-0307.html

In particular, you can give your subclass a __reduce__ implementation, which
pretty much gives you complete control over how it is pickled and unpickled.

---
Greg Chapman




More information about the Python-list mailing list