Best way to add attributes to shelved objects?

Jonathon McKitrick jcm at FreeBSD-uk.eu.org
Mon Apr 26 12:02:26 EDT 2004


On Mon, Apr 26, 2004 at 10:48:33AM -0500, Skip Montanaro wrote:
:     skip> Why not check for those attributes in your __getstate__ method and
:     skip> add default (or computed) values for any missing attributes?
: 
:     jm> I tried experimenting with these, and could not get them to work.
:     ...
:     jm> Are there any examples on how to do this?
: 
: How about:
: 
:     class Foo:
:         def __init__(self):
:             self.a = 1
: 
:         def __setstate__(self, d):
:             if 'b' not in d:
:                 d['b'] = 2
:             self.__dict__ = d

Perfect!  Just what I was looking for.  I was just doing the wrong thing in
setstate.  Thanks!


jm
-- 
My other computer is your Windows box.




More information about the Python-list mailing list