On 03/06/2019 10:30 AM, duncan smith wrote:

 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])
None
None
None
--> 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'

It seems to me the problem only exists because you are trying to print 
`self.shape` -- stop printing it, and everything works normally.

What problem are you actually trying to solve?

--
~Ethan~

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to