Search code examples
pythongoogle-app-enginesessionglobal-variablesonchange

Detecting change in the value of python variables


Is there some way to call a function each-time the value of a variable changes, in python?

Something like a listener, perhaps?

Specifically, I'm referring to the case where only the variable is shared between scripts like a GAE-Session. (Which uses Cookies, Memcache, etc to share data)

Example: ScriptA & ScriptB, share a session variable. When Script B changes it, SctiptA has to call a method to handle that change.


Solution

  • Use properties. Only mutable values can change to begin with.

    class Something(object):
        @property
        def some_value(self):
            return self._actual
        @some_value.setter
        def some_value(self, value):
            print ("some_value changed to", value)
            self._actual = value