slice object unpickable

Sebastien de Menten sdementen at
Tue Nov 2 11:29:15 CET 2004


I wonder why the slice object is not pickable via pickle or cPickle.

import pickle

leads to

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "/usr/lib/python2.3/", line 231, in dump
  File "/usr/lib/python2.3/", line 313, in save
    rv = reduce(self.proto)
  File "/usr/lib/python2.3/", line 69, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle slice objects

Is it an omission ? Am I missing something non trivial that would
impede slices to be pickable ?

My main goal is to pickle an object with some slices as attributes. As
my object is quite simple, I would like to enjoy the automatic pickle
system (i.e. not rewrite a __getstate__, __setstate__, etc).
Is there an easy workaround to the "unpickableness" of slices ? I
tried subclassing slices but it is not allowed.

Thanks for your help.


More information about the Python-list mailing list