views:

43

answers:

1

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 safety measure, taken just in case self.__dict__ contains some data when __setstate__ is called? or is it simply faster and cleaner than doing

self.__dict__ = dict_returned_by_pickle

which forces the old self.__dict__ to be garbage collected?

A: 

Replacing it is fine. That is what the borg pattern does.

The original dict will be garbage collected if there are no other references to it.

gnibbler