I want to have classes that automatically send notifications to subscribers whenever one of their attributes change. So if I would write this code:
JavaScript
x
14
14
1
@ChangeMonitor
2
class ChangingClass(object):
3
4
def __init__(self, x):
5
self.x = x
6
7
8
changer = ChangingClass(5)
9
print("Going to change x.")
10
changer.x = 6
11
print("Going to not change x.")
12
changer.x = 6
13
print("End of program")
14
The output would be:
JavaScript
1
5
1
Going to change x
2
Old x = 5, new x = 6
3
Going to not change x.
4
End of program.
5
My question is how to implement the ChangeMonitor decorator class. In the above example I assume it will print a line indicating the changes of an attribute, but for useful purposes it could send notifications to subscribed objects.
Advertisement
Answer
You’d have to add a __setattr__()
method:
JavaScript
1
17
17
1
def ChangeMonitor(cls):
2
_sentinel = object()
3
old_setattr = getattr(cls, '__setattr__', None)
4
def __setattr__(self, name, value):
5
old = getattr(self, name, _sentinel)
6
if old not is _sentinel and old != value:
7
print "Old {0} = {1!r}, new {0} = {2!r}".format(name, old, value)
8
if old_setattr:
9
old_setattr(self, name, value)
10
else:
11
# Old-style class
12
self.__dict__[name] = value
13
14
cls.__setattr__ = __setattr__
15
16
return cls
17
This should handle existing __setattr__
hooks as well. The _sentinel
is used to allow None
as the old value too.
Demo:
JavaScript
1
11
11
1
>>> changer = ChangingClass(5)
2
>>> changer.x = 6
3
Old x = 5, new x = 6
4
>>> changer.x = 6
5
>>> # nothing printed
6
7
>>> changer.x = None
8
Old x = 6, new x = None
9
>>> changer.x = 6
10
Old x = None, new x = 6
11