Python variable value detection

Is there a way to call a function every time the value of a variable changes in python?

Something like a listener, maybe?

In particular, I mean the case when only a variable is available . between scripts such as GAE-Session. (Which uses cookies, memcache, etc. to exchange data)

Example: ScriptA and ScriptB share a session variable. When Script B modifies it, SctiptA must call a method to handle this change.

+1
source share
5 answers

. .

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
+3
+2
+1

( , )

def set_myvar(x):
    my_var = x
    some_function()
    return my_var
0

, "int" "float" "setattr", -.

class MyInt(int):
    ...
    def __setattr__(self, attr, val):
        ...
        super(MyInt, self).__setattr__(attr, val)
        ...

var = MyInt(5)
0

Source: https://habr.com/ru/post/1606325/


All Articles