serialization and versioning

Robert Kern robert.kern at gmail.com
Fri Oct 12 09:26:19 EDT 2012


On 10/12/12 11:42 AM, Neal Becker wrote:
> I wonder if there is a recommended approach to handle this issue.
>
> Suppose objects of a class C are serialized using python standard pickling.
> Later, suppose class C is changed, perhaps by adding a data member and a new
> constructor argument.
>
> It would see the pickling protocol does not directly provide for this - but is
> there a recommended method?
>
> I could imagine that a class could include a class __version__ property that
> might be useful - although I would further expect that it would not have been
> defined in the original version of class C (but only as an afterthought when it
> became necessary).

You don't need to add anything to the actual attributes on the class. Just 
define your __getstate__() dict to include a '__version__' item. You can treat 
the absence of '__version__' in the dict that __setstate__() receives as 
implying "version 0".

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list