Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting an error about my class defining __slots__ when trying to pickle an object?
    text
    copied!<p>I'm trying to pickle an object of a (new-style) class I defined. But I'm getting the following error:</p> <pre><code>&gt;&gt;&gt; with open('temp/connection.pickle','w') as f: ... pickle.dump(c,f) ... Traceback (most recent call last): File "&lt;stdin&gt;", line 2, in &lt;module&gt; File "/usr/lib/python2.5/pickle.py", line 1362, in dump Pickler(file, protocol).dump(obj) File "/usr/lib/python2.5/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.5/pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "/usr/lib/python2.5/pickle.py", line 419, in save_reduce save(state) File "/usr/lib/python2.5/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.5/pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems save(v) File "/usr/lib/python2.5/pickle.py", line 306, in save rv = reduce(self.proto) File "/usr/lib/python2.5/copy_reg.py", line 76, in _reduce_ex raise TypeError("a class that defines __slots__ without " TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled </code></pre> <p>I didn't explicitly define <code>__slots__</code> in my class. Did something I do implicitly define it? How do I work around this? Do I need to define <code>__getstate__</code>?</p> <p><strong>Update:</strong> <a href="https://stackoverflow.com/users/174728/gnibbler">gnibbler</a> chose a good example. The class of the object I'm trying to pickle wraps a socket. (It occurs to me now that) sockets define <code>__slots__</code> and not <code>__getstate__</code> for good reason. I assume once a process ends, another process can't unpickle and use the previous process's socket connection. So while I'm accepting <a href="https://stackoverflow.com/users/95810/alex-martelli">Alex Martelli</a>'s excellent answer, I'm going to have to pursue a different strategy than pickling to "share" the object reference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload