我有一个数据(即{'/domflight': 10, '/intlflight': 20}
),想将'/domflight'
的值修改为50
。下面是我的代码,我正在尝试做,但没有运气。在
import shelve
s = shelve.open('/tmp/test_shelf.db')
try:
print s['key1']
s['key1']['/domflight'] = 50
finally:
s.close()
s = shelve.open('/tmp/test_shelf.db', writeback=True)
try:
print s['key1']
finally:
s.close()
搁置无法检测对嵌套可变对象的更改。在嵌套字典中设置键不会触发保存。在
请重新设置词典:
是对
s['key1']
的赋值触发了保存。在从技术上讲,
s
是UserDict.DictMixin
类的一个子类,具有自定义的__setitem__
方法。直接分配给s
对象中的键将调用该方法并保存更改。但是分配给一个嵌套在键下的可变对象不会触发对__setitem__
的调用,因此不会检测到更改,也不会保存任何内容。在这是covered in the documentation:
相关问题 更多 >
编程相关推荐