Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 821
Nolhian Avatar asked Dec 19 '10 06:12

Nolhian


1 Answers

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()
like image 116
Karl Knechtel Avatar answered Oct 05 '22 05:10

Karl Knechtel