特性列表未报告已添加或删除的项目 [英] Traits List not reporting items added or removed
问题描述
给出
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>
文档说我应该获得添加/删除的项目的列表.
The documentation says I should get a list of items added/removed.
我在这里想念什么?这是Windows 8上的特征4.3.
What am I missing here? This is traits 4.3 on windows 8.
谢谢!
推荐答案
更改集合整体的值(List
)和更改内部成员 之间似乎存在区别. >集合.追加似乎会更改其中的成员(或至少会导致相同的通知).如果您整体上更改容器的值,则确实可以将更改后的列表作为new
值获得:
There seems to be a distinction between changing the value of the collection as a whole (the List
) and changing a member within the collection. Appending appears to change a member within (or at least results in the same notification). If you change the value of the container as a whole, you do indeed get the changed list as the new
value:
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>
There's more info here, if you haven't seen this section already. See this answer too.
这篇关于特性列表未报告已添加或删除的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!