python - Notify parent instance about property change -
i have these 2 classes:
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): # should called when self.status.message changed
and use them this:
buddy = buddy('john') buddy.status.message = 'hello world!' # should call buddy.status_updated
i want buddy.status_updated
called when modify message
property of status
. how achieve this?
you'll have store reference back parent; python values not track stored (there can multiple places refer status()
instances):
class status(object): def __init__(self, parent=none): self._message = '' self._parent = parent @property def message(self): return self._message @message.setter def message(self, value): self._message = value if self._parent not none: self._parent.status_updated() class buddy(object): def __init__(self, name): self.name = name self.status = status(self) def status_updated(self): # should called when self.status.message changed
Comments
Post a Comment