How to pickle a subclass of tuple?
Shalabh Chaturvedi
shalabh at cafepy.com
Thu Jun 3 20:52:45 EDT 2004
Christos TZOTZIOY Georgiou 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?
Why do you need to do this i.e. redefine __getstate__() and __setstate__()?
Doesn't just pickling instances of your class work?
>>> class T(tuple):pass
...
>>> t = T((1,2))
>>> t.a = 33
>>> import pickle
>>> x = pickle.loads(pickle.dumps(t))
>>> x
(1, 2)
>>> x.a
33
>>>
--
Shalabh
More information about the Python-list
mailing list