Best way to add attributes to shelved objects?

Jonathon McKitrick jcm at FreeBSD-uk.eu.org
Mon Apr 26 11:05:35 EDT 2004


On Fri, Apr 23, 2004 at 02:49:43PM -0500, Skip Montanaro wrote:
: 
:     jm> I'm working on an app that persists a few classes with shelve.  But
:     jm> if I load older saved classes, I get an exception in code that uses
:     jm> the new attributes, of course.
: 
:     jm> What is the best way to load these older classes, check for the new
:     jm> attributes, and add them so they will work with the new code?
: 
: Why not check for those attributes in your __getstate__ method and add
: default (or computed) values for any missing attributes?

I tried experimenting with these, and could not get them to work.
I had main() create a class, then shelve it.  Then I added a new attribute
to the class source code, and a statement to print it.  When I tried hooking
in __setstate__ and/or __getstate__ to load __dict__, it still did not find
the new attribute when loading.

Are there any examples on how to do this?


jm
-- 
My other computer is your Windows box.




More information about the Python-list mailing list