creating a class that behaves like any variable bu

2019-06-24 07:54发布

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,

4条回答
冷血范
2楼-- · 2019-06-24 08:49

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:

from collections import namedtuple

CallbackVar = namedtuple('CallbackVar', ['change_callback', 'read_callback'])

class CallbackDict(dict):
    'Variant of dict that does callbacks for instances of CallbackVar'
    def __getitem__(self, key):
        value = dict.__getitem__(self, key)
        if isinstance(value, CallbackVar):
            return value.read_callback(key)
    def __setitem__(self, key, value):
        try:
            realvalue = dict.__getitem__(self, key)
            if isinstance(realvalue, CallbackVar):
                return realvalue.change_callback(key, value)
        except KeyError:
            pass
        return dict.__setitem__(self, key, value)

stmts = '''
x = CallbackVar(setter, getter)     # Attach getter() and setter() to "x"
x = 1                               # Invoke the setter()
x                                   # Invoke the getter()
'''

def getter(key):
    print 'Getting', key
    return 42

def setter(key, value):
    print 'Setting', key, 'to', value

exec stmts in globals(), CallbackDict()
查看更多
混吃等死
3楼-- · 2019-06-24 08:49

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.

查看更多
Melony?
4楼-- · 2019-06-24 08:50

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.

查看更多
小情绪 Triste *
5楼-- · 2019-06-24 08:54

Variable is a reference to an python object. It's wrong to say that "a class behave like a variable". when you do

x = [1, 2, 3]

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 this

class A(object):
    def __init__(self): 
        self._x = 3
    @property
    def x():
        read_callback() # called!
        return self._x
    ....
a = A()
print a.x

If this is what you what.

查看更多
登录 后发表回答