pickle/unpickle class which has changed

Peter Otten __peter__ at web.de
Tue Mar 6 08:52:11 EST 2012


Neal Becker wrote:

> What happens if I pickle a class, and later unpickle it where the class
> now has added some new attributes?

- If the added attributes' values are immutable, provide defaults as class 
attributes.

- Implement an appropriate __setstate__() method. The easiest would be

# untested
def __setstate__(self, state):
    self.__dict__.update(newattr1=42, newattr2=[])
    self.__dict__.update(state)




More information about the Python-list mailing list