I would like to create a class that behaves like a python variable, but calls some callback function when the "variable" changes / is read.
In other words, I would like to be able to use the class as follows:
x=myClass(change_callback, read_callback)
defines x as an instance of myclass. The constructor ( INIT ) accepts 2 functions as a paramater: a function that needs to be called every time x changes, and a function that needs to be called every time x is "read"
The following statement:
x=1
saves 1 and calls change_callback(1) , which can do something.
The following statement:
a=x
will retrieve the stored value and call read_callback() , which may change the stored value and do other things.
I would like this to work with any type, for example. to write things like:
x=[1 2 3] , which will cause change_callback([1 2 3])
x.append(4) runs change_callback([1 2 3 4]) (and possibly calling read_callback() before)
x={'a':1} will call change_callback({'a':1})
print(x) will call read_callback () ... and, of course, will return the last stored value for printing. A.
The idea is that any access to a variable can be logged or generate other calculations ... without visible results.
I have a feeling that it should be possible, but I really donβt see what my object should inherit from ... If I need to limit me to one type, for example. list, is there a way to override all assignment operators (including methods such as append () ...) "once", preserving the original behavior (base class method) and adding a callback ...
Or are there more suitable ways (modules ...) to achieve the same goals ...?
Thanks in advance,