[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

in a pickle

      I've been trying to figure out why one of my classes can be
pickled but not unpickled. (I realise the problem is probably with the
pickling, but I get the error when I attempt to unpickle.)

A relatively minimal example is pasted below.

>>> import pickle
>>> class test(dict):
	def __init__(self, keys, shape=None):
		self.shape = shape
		for key in keys:
			self[key] = None

	def __setitem__(self, key, val):
		print (self.shape)
		dict.__setitem__(self, key, val)

>>> x = test([1,2,3])
>>> s = pickle.dumps(x)
>>> y = pickle.loads(s)
Traceback (most recent call last):
  File "<pyshell#114>", line 1, in <module>
    y = pickle.loads(s)
  File "<pyshell#111>", line 8, in __setitem__
    print (self.shape)
AttributeError: 'test' object has no attribute 'shape'

I have DUCkDuckGo'ed the issue and have tinkered with __getnewargs__ and
__getnewargs_ex__ without being able to figure out exactly what's going
on. If I comment out the print call, then it seems to be fine. I'd
appreciate any pointers to the underlying problem. I have one or two
other things I can do to try to isolate the issue further, but I think
the example is perhaps small enough that someone in the know could spot
the problem at a glance. Cheers.