Watching a property for changes
I need a function similar to 开发者_开发百科gobject.io_add_watch
but for a variable. For example it needs to watch the variable stop
initialized to stop = False
and when stop is changed to True
it must call a function. I can't have a separate thread watching the variable in a loop with a time.sleep.
Is there such a function or a way to do that ?
Use a property in a class:
class Stopwatch(object):
def __init__(self, callback):
self._stop = False
self.callback = callback
@property
def stop(self): return self._stop
@stop.setter
def stop(self, value):
self._stop = value
if value: self.callback()
精彩评论