装饰一个类来监控属性变化

2023-11-26

我希望有一些类,只要订户的属性之一发生更改,就会自动向订户发送通知。所以如果我写这段代码:

@ChangeMonitor
class ChangingClass(object):

    def __init__(self, x):
        self.x = x


changer = ChangingClass(5)
print("Going to change x.")
changer.x = 6
print("Going to not change x.")
changer.x = 6
print("End of program")

输出将是:

Going to change x
Old x = 5, new x = 6
Going to not change x.
End of program.

我的问题是如何实现 ChangeMonitor 装饰器类。在上面的示例中,我假设它将打印一行指示属性的更改,但出于有用的目的,它可以向订阅的对象发送通知。


你必须添加一个__setattr__() method:

def ChangeMonitor(cls):
    _sentinel = object()
    old_setattr = getattr(cls, '__setattr__', None)
    def __setattr__(self, name, value):
        old = getattr(self, name, _sentinel)
        if old not is _sentinel and old != value:
            print "Old {0} = {1!r}, new {0} = {2!r}".format(name, old, value)
        if old_setattr:
            old_setattr(self, name, value)
        else:
            # Old-style class
            self.__dict__[name] = value

    cls.__setattr__ = __setattr__

    return cls

这应该处理现有的__setattr__钩子也是如此。这_sentinel用于允许None也与旧值一样。

Demo:

>>> changer = ChangingClass(5)
>>> changer.x = 6
Old x = 5, new x = 6
>>> changer.x = 6
>>> # nothing printed
...
>>> changer.x = None
Old x = 6, new x = None
>>> changer.x = 6
Old x = None, new x = 6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

装饰一个类来监控属性变化 的相关文章

随机推荐