[Zope-dev] The Application object

Chris McDonough chrism@digicool.com
Mon, 17 Jul 2000 14:50:48 -0400


How about a class attribute?

class MyClass:

    myAttr2 = 'Default'

    def getMyAttr1(self):
        return self.myAttr1

    def getMyAttr2(self):
        return self.myAttr2

If myAttr2 needs to be a ZClass instance, just grab it inside the class.

> -----Original Message-----
> From: Jeff K. Hoffman [mailto:jeff.hoffman@goingv.com]
> Sent: Monday, July 17, 2000 2:44 PM
> To: Chris McDonough
> Cc: 'Jeff K. Hoffman'; Shane Hathaway; zope-dev@zope.org
> Subject: RE: [Zope-dev] The Application object
> 
> 
> 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/