class versions and object deserialization

Ian Kelly ian.g.kelly at gmail.com
Tue Apr 24 15:16:22 EDT 2012


On Tue, Apr 24, 2012 at 1:02 AM, J. Mwebaze <jmwebaze at gmail.com> wrote:
> We have classes of this form classA version1, classA version2, classA
> version3 .. etc. This is same class that has been modified. Each
> "modification" creates a new version of a class. Each object has a version
> attribute which refers to the version of the class from which it was
> derived. egObjectA.version =1 # means it was derived from ClassA version1

Note that each object already has a __class__ attribute containing the
class it is an instance of, so unless you have special need of version
number metadata, it may not be necessary.

> Here is my problem. During object de-serializing, i would like to use the
> specific version of the class that was used to used to make the object. For
> example, if i am de-serializing object ObjectA withversion=3 then ClassA
> version 3 should be used. Source code for all the different variations of
> the classes is stored.

What serialization scheme are you using, and how are the classes
stored?  If you're using pickle and you keep them accessible from the
module they were defined in using the name they were defined with,
then you won't need to do anything special here -- pickle will look up
that name in that module and find the class.  If you're doing
something more creative, then you'll want to override the __reduce__
method in your classes to pass in a custom constructor that takes into
account the version number.



More information about the Python-list mailing list