pickle/unpickle class which has changed

Gelonida N gelonida at gmail.com
Tue Mar 6 19:22:46 EST 2012


Hi Peter,

A related question.


Is there anyhing like a built in signature which would help to detect,
that one tries to unpickle an object whose byte code has changed?

The idea is to distinguish old and new pickled data and start some
'migration code' fi required


The only thing, that I thought about so far was adding an explicit
version number to each class in order to detect such situations.



On 03/06/2012 02:52 PM, Peter Otten wrote:
> 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