slice object unpickable

  • Thread starter Sebastien de Menten
  • Start date
S

Sebastien de Menten

Hi,

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

import pickle
pickle.dumps(slice(0,4))

leads to

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/pickle.py", line 1386, in dumps
Pickler(file, protocol, bin).dump(obj)
File "/usr/lib/python2.3/pickle.py", line 231, in dump
self.save(obj)
File "/usr/lib/python2.3/pickle.py", line 313, in save
rv = reduce(self.proto)
File "/usr/lib/python2.3/copy_reg.py", 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.

Seb
 
J

Josiah Carlson

Hi,

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

[snip slice pickling]

I don't know why clice pickling was disabled.

To get past it, why not just store the argument tuples that lead to
slices? (sli.start, sli.stop, sli.step)

- Josiah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top