Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

serialization and versioning

47 views
Skip to first unread message

Neal Becker

unread,
Oct 12, 2012, 6:42:03 AM10/12/12
to pytho...@python.org
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).

Etienne Robillard

unread,
Oct 12, 2012, 7:04:37 AM10/12/12
to Neal Becker, pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list

i guess a easy answer is to say to try python 3.3 but how would this translate in
python (2) code ?

Neal Becker

unread,
Oct 12, 2012, 8:50:56 AM10/12/12
to pytho...@python.org
So are you saying python 3.3 has such a feature? Where is it described?

Robert Kern

unread,
Oct 12, 2012, 9:26:19 AM10/12/12
to pytho...@python.org
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

Dieter Maurer

unread,
Oct 13, 2012, 2:23:14 AM10/13/12
to pytho...@python.org
Neal Becker <ndbe...@gmail.com> writes:

> 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).

The ZODB (Zope Object DataBase) is based on Python's pickle.

In the ZODB world, the following strategy is used:

* if the class adds a new data attribute, give it (in addition) a
corresponding class level attribute acting as "default" value
in case the pickled state of an instance lacks this
instance level attribute

* for more difficult cases, define an appropriate "__getstate__"
for the class that handles the necessary model upgrades

0 new messages