Detect when a class member is updated

Jørgen Cederberg jorgencederberg at hotmail.com
Wed Jan 15 15:53:45 CET 2003


Boethius wrote:
> Can I detect when a class member is added or updated?
> 
> I have a class with a member called path. Everytime it's updated, I'd
> like to run a 'sanity check' on the new value.
> 
> How can I do this?

How about using properties, as this example shows. You could replace the 
notify method with a validate method instead.

class Detect(object):
     __value = 0
     def set_value(self, value):
          self.__value = value
          self.notify()
     def get_value(self):
          return self.__value
     def notify(self):
          print "Value was changed to", str(self.value)
     value = property(get_value, set_value)

Regards
Jorgen





More information about the Python-list mailing list