I would like to create a class that behaves as a python variable but calls some callback function when the "variable" is changed/read.
In other words, I'd 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) takes 2 functions as paramater: a function to be called each time x is changed, and a function to be called each time x is "read"
The following statement:
x=1
would "save" 1 and trigger a call to change_callback(1)
which could do anything.
The following statement:
a=x
would retrieve the stored value and call read_callback()
which would possibly change the stored value and do other things.
I would like this to work with any type, e.g. to be able to write things like:
x=[1 2 3]
which would trigger change_callback([1 2 3])
x.append(4)
would trigger change_callback([1 2 3 4])
(and possibly a call to read_callback()
before)
x={'a':1}
would trigger change_callback({'a':1})
print(x)
would trigger a call to read_callback()...and return the last stored value for printing, of course.
The idea is that any access to the variable could be logged, or generate other calculation... seemlessly.
I get the feeling this should be possible, but I don't really see what my object should inherit from... If I have to restrict me to one type,e.g. a list, is there any way to redefine all assignment operators (including methods like append()...) in "one go", keeping the original behaviour (the base class method) and adding the callback...
Or are there more appropriate ways (modules...) to achieve the same goals...?
Thanks in advance,
Objects don't know when they get assigned to a variable. Writing
x = a
adds a dict entry (or locals entry) that points to a. The a object doesn't get notified (though, in CPython, its reference count gets incremented).The part that does get notified is the container where the object is assigned. In the case of a global assignment, the module dictionary gets updated. In the case of instance variable updates like
a.b = x
, there is a dotted lookup and store to the instance dictionary.You can make those containers invoke arbitrary code on the lookup or store. Python's property provides this capability to new-style classes. The exec and eval operations let you specify a custom dict that can provide this capability to regular assignments. In both cases, you are in complete control of what happens upon assignment.
Summary: Lookup and store behaviors are controllable through the target of the assignment rather than the object being assigned.
Example:
You can't do this with the
=
operator - since by definition, it's designed to overwrite the left-hand side, whatever the current contents of that are don't get any notification that such is happening. (And the assignment operator is not override-able in Python.)What you could do, however, is use
.set()
and.get()
methods on your class. It wouldn't get you quite the same "magic", but honestly, that's probably a good thing - it makes it clearer what's going on.You might like to look at descriptors: http://docs.python.org/howto/descriptor.html
This will only work for object properties, though, which is probably enough for you.
Variable is a reference to an python object. It's wrong to say that "a class behave like a variable". when you do
the variable
x
will be a reference(redirected) to the new list object[1, 2, 3]
, not your old object was "changed". It just lost a reference.To do what you may want. Try to call
change_callback
in the__init__
method of your class.To have the
read_callback
called. Do something like thisIf this is what you what.