我将让下面的终端会话自己发言:
>>> import shelve
>>> s = shelve.open('TestShelve')
>>> from collections import deque
>>> s['store'] = deque()
>>> d = s['store']
>>> print s['store']
deque([])
>>> print d
deque([])
>>> s['store'].appendleft('Teststr')
>>> d.appendleft('Teststr')
>>> print s['store']
deque([])
>>> print d
deque(['Teststr'])
d
和{appendleft
对d
起作用,而对{
结果发现它们不一样,因此对它们执行的任何操作都不匹配:
要在编码时修改项目,需要传递参数
^{pr2}$writeback=True
:请参阅文档:
您也可以使用
writeback=False
来执行此操作,但是您需要按照提供的示例编写代码:shelve
正在pickle
对对象进行序列化。当然,这是复制品。因此,从shelve
返回的对象将与您放入的对象具有不同的标识,尽管它们将是等效的。在如果这很重要,您可以编写一个
deque
子类,每当它被修改时,它会自动重新上架,尽管在许多用例中这可能会有很差的性能。在相关问题 更多 >
编程相关推荐