setstate

Custom instance unpickling in Python: should the object dictionary be updated, or is replacing it OK?

When defining how objects of a certain class should be unpickled, via __setstate__, I gather that it is safe to do def __setstate__(self, dict_returned_by_pickle): self.__dict__.update(dict_returned_by_pickle) when the pickled state is a dictionary. This is what I have seen in an answer here on stackoverflow. However, is this a ...

How to pickle and unpickle objects with self-references and from a class with slots?

What is a correct way to pickle an object from a class with slots, when this object references itself through one of its attributes? Here is a simple example, with my current implementation, which I'm not sure is 100 % correct: import weakref import pickle class my_class(object): __slots__ = ('an_int', 'ref_to_self', '__weakref__...