鉴于,
from enthought.traits.api import HasTraits, Tuple, Delegate, Trait, Float,Dict,List
class Foo(HasTraits):
def __init__(self):
super(Foo,self).__init__()
self.add_trait('node',List)
def _node_items_changed(self,name,old,new):
print name
print old
print new
为什么我会:
>>> f = Foo()
>>> f.node.append(0)
node_items
<undefined>
<traits.trait_handlers.TraitListEvent object at 0x05BA8CF0>
文档说我应该添加/删除项目列表。
我在这里缺少什么?这是Windows 8上的特征4.3。
谢谢!
答案 0 :(得分:2)
在集合中更改集合的值(List
)和更改集合中的成员之间似乎存在区别。追加似乎会更改其中的成员(或至少导致相同的通知)。如果您更改整个容器的值,确实将更改后的列表作为new
值:
from enthought.traits.api import HasTraits, Tuple, Delegate, Trait, Float,Dict,List
class Foo(HasTraits):
def __init__(self):
super(Foo,self).__init__()
self.add_trait('node',List)
def _node_changed(self,name,old,new):
print("_node_changed: %s %s %s" % (name, str(old), str(new)))
def _node_items_changed(self,name,old,new):
print("_node_items_changed: %s %s %s" % (name, str(old), str(new)))
f = Foo()
# change the List membership with append:
f.node.append(0)
# _node_items_changed: node_items <undefined> <traits.trait_handlers.TraitListEvent object at 0x10128af50>
# change the List itself:
f.node = [1,2,3]
# _node_changed: node [0] [1, 2, 3]
# change a member (same result as append):
f.node[1] = 4
# _node_items_changed: node_items <undefined> <traits.trait_handlers.TraitListEvent object at 0x10128af50>