[Zope3-Users] Syncing two attributes

Shane Hathaway shane at hathawaymix.org
Fri Jun 24 15:46:53 EDT 2005


Florian Lindner wrote:
> Hello,
> I've one attribute A which is exposed via the Interface. Another internal 
> attribute B should be in sync with it. So everytime A changes I want a 
> function to be called. 
> 
> I think (solving another error stands before testing it) with __setattr__:
> 
>     def __setattr__(self, name, value):
>         if name == "expirationTime":
>             self.expTimeDelta = datetime.timedelta(minutes = value)
>         
>         self.__dict__[name] = value
> 
> But I really don't like that way? Is there elegant, more zope-like solution? 
> Or is this the way to do it?

Would a property work?

class MyClass(object):

    def getExpirationTime(self):
        return self._expirationTime

    def setExpirationTime(self, value):
        self.expTimeDelta = datetime.timedelta(minutes = value)
        self._expirationTime = value

    expirationTime = property(getExpirationTime, setExpirationTime)

Shane


More information about the Zope3-users mailing list