【发布时间】:2015-02-15 06:39:30
【问题描述】:
假设我有一个非常简单的数据类型:
class SimpleObject:
def __init__(self, property):
self.property = property
def update_property(self, value):
self.property = value
还有一种特殊的列表来存储数据类型:
class SimpleList(collections.MutableSequence):
def update_useful_property_of_list(self, value):
self.useful_property_of_list = value
我存储它们:
simple1 = SimpleObject(1)
simple2 = SimpleObject(2)
simple_list = SimpleList([simple1, simple2])
SimpleList 对象有什么方法可以知道其成员的属性之一何时发生变化?例如,当发生这种情况时,如何让simple_list 执行self.update_useful_property_of_list():
simple1.update_property(3)
【问题讨论】:
-
我记得函数装饰器是处理这个问题的一种方式。这可能是一个值得研究的方向。
-
它是观察者设计模式,您可以通过快速搜索轻松找到有关它的资源。
-
你想让它做什么?
-
@PadraicCunningham 我已经更新了这个问题,以举一个简单的例子来说明我想要做什么。
-
我认为类或静态方法实际上可能更接近你想要的
标签: python list sequence observer-pattern mutable