[Zope-dev] The Application object
Jeff K. Hoffman
jeff.hoffman@goingv.com
Mon, 17 Jul 2000 14:44:24 -0400 (EDT)
On Mon, 17 Jul 2000, Chris McDonough wrote:
> Jeff, what exactly do you need to do with setstate? can you provide an
> example?
I am simply trying to "upgrade" a persistent instance of a Python class.
For instance:
class MyClass:
def __init__(self, id, title=''):
self.myAttr1 = 'Value 1'
def getMyAttr1(self):
return self.myAttr1
Imagine that I use the class, as defined above, for a while, then
decide I need to modify it as follows:
class MyClass:
def __init__(self, id, title=''):
self.myAttr1 = 'Value 1'
self.myAttr2 = 'Value 2'
def getMyAttr1(self):
return self.myAttr1
def getMyAttr2(self):
return self.myAttr2
I already have a few instances of MyClass in the ZODB, and they only have
myAttr1; myAttr2 is not defined on existing instances, since they will not
be constructed via the new __init__. Therefore, getMyAttr2() will throw an
exception.
The simple solution is to also define __setstate__:
def __setstate__(self, state):
Persistent.__setstate__(self, state)
if not hasattr(self, 'myAttr2'):
self.myAttr2 = 'Value 2'
Now, when the old instances of my object are de-ghosted from the ZODB,
__setstate__ notices that they don't have a myAttr2, and sets it to the
value normally given by the constructor.
Unfortunately, in my case, 'Value 2' is an instance of a ZClass.
Therefore, I need to get at Control_Panel.
Hope this helps. Now I'm going to try Shane's wacky __of__ idea. :-)
--Jeff
---
Jeff K. Hoffman 704.849.0731 x108
Chief Technology Officer mailto:jeff.hoffman@goingv.com
Going Virtual, L.L.C. http://www.goingv.com/