サブスクライバーの属性の 1 つが変更されるたびに、サブスクライバーに自動的に通知を送信するクラスが必要です。したがって、このコードを書くとしたら:
@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 デコレータ クラスを実装する方法です。上記の例では、属性の変更を示す行を出力すると想定していますが、有用な目的のために、サブスクライブされたオブジェクトに通知を送信できます。