我有这两个类:
class Status(object):
def __init__(self):
self._message = ''
@property
def message(self):
return self._message
@message.setter
def message(self, value):
self._message = value
class Buddy(object):
def __init__(self, name):
self.name = name
self.status = Status()
def status_updated(self):
# this should be called when self.status.message is changed
我像这样使用它们:
buddy = Buddy('John')
buddy.status.message = 'Hello world!' # this should call Buddy.status_updated
我想Buddy.status_updated
在修改 的message
属性时被调用Status
。如何做到这一点?