Why am I getting an error about my class defining __slots__ when trying to pickle an object? Why am I getting an error about my class defining __slots__ when trying to pickle an object? python python

Why am I getting an error about my class defining __slots__ when trying to pickle an object?


The class defining __slots__ (and not __getstate__) can be either an ancestor class of yours, or a class (or ancestor class) of an attribute or item of yours, directly or indirectly: essentially, the class of any object in the directed graph of references with your object as root, since pickling needs to save the entire graph.

A simple solution to your quandary is to use protocol -1, which means "the best protocol pickle can use"; the default is an ancient ASCII-based protocol which imposes this limitation about __slots__ vs __getstate__. Consider:

>>> class sic(object):...   __slots__ = 'a', 'b'... >>> import pickle>>> pickle.dumps(sic(), -1)'\x80\x02c__main__\nsic\nq\x00)\x81q\x01.'>>> pickle.dumps(sic())Traceback (most recent call last):  [snip snip]    raise TypeError("a class that defines __slots__ without "TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled>>> 

As you see, protocol -1 takes the __slots__ in stride, while the default protocol gives the same exception you saw.

The issues with protocol -1: it produces a binary string/file, rather than an ASCII one like the default protocol; the resulting pickled file would not be loadable by sufficiently ancient versions of Python. Advantages, besides the key one wrt __slots__, include more compact results, and better performance.

If you're forced to use the default protocol, then you'll need to identify exactly which class is giving you trouble and exactly why. We can discuss strategies if this is the case (but if you can possibly use the -1 protocol, that's so much better that it's not worth discussing;-) and simple code inspection looking for the troublesome class/object is proving too complicated (I have in mind some deepcopy-based tricks to get a usable representation of the whole graph, in case you're wondering).


Perhaps an attribute of your instance is using __slots__

For example, socket has __slots__ so it can't be pickled

You need to identify which attribute is causing the error and write your own__getstate__ and __setstate__ to ignore that attribute


From PEP 307:

The __getstate__ method should return a picklable value representing the object's state without referencing the object itself. If no __getstate__ method exists, a default implementation is used that returns self.__dict__.