基本上,我尝试pickle并恢复Traits对象,它包含一个对象列表。 还原时,将触发两次有关列表更改的事件通知:
from enthought.traits.api import Int, HasTraits, List, on_trait_change
import cPickle
class Foo(HasTraits):
a = Int(1)
class Bar(HasTraits):
l = List(Foo)
@on_trait_change('l[]')
def _l_changed(self):
for x in self.l:
print x.a
def store(self):
file = open('test_state.pyd', 'w')
cPickle.dump(self.__getstate__(), file)
file.close()
def restore(self):
file = open('test_state.pyd')
self.__setstate__(cPickle.load(file))
file.close()
b = Bar(l=[Foo()])
b.store()
print 'restoring...'
b.restore()
将导致恢复。。。11
我认为问题是pickle不仅pickle对象b的属性,而且pickle还pickle对象b本身(因为列表)。 这个问题也已经在这里指出了https://mail.enthought.com/pipermail/enthought-dev/2006-December/003707.html,尽管没有答案。 然后打电话来cPickle.load公司因此,它将已经创建一个Bar对象,并触发事件。 setstate方法将在当前对象b上设置pickled数据,再次触发事件。在
有没有想过如何避免这种情况,即第一次cPickle.load公司(文件)?在
目前没有回答
相关问题 更多 >
编程相关推荐