时间:2021-07-01 10:21:17 帮助过:22人阅读
具体实现方法如下:
import shelve she = shelve.open('try.she','c') for c in 'spam': she[c] = {c:23} for c in she.keys(): print c,she[c] she.close() she = shelve.open('try.she','c') print she['p'] she['p']['p'] = 42 #这样修改是不行,这只是修改了个临时对象 print she['p'] a = she['p']#给临时对象绑定个名字 a['p'] = 42 she['p'] = a print she['p']
本文实例测试环境为Python2.7.6
程序运行结果如下:
p {'p': 23} a {'a': 23} m {'m': 23} s {'s': 23} {'p': 23}#原值是这样的 {'p': 23}#只是修改了临时对象 {'p': 42}#绑定名字后,达到修改的目的
实例代码及运行结果均配有较为详尽的注释,帮助大家理解其含义。希望本文所述对大家的Python程序设计有所帮助。