代码:
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']
结果:
p {'p': 23}
a {'a': 23}
m {'m': 23}
s {'s': 23}
{'p': 23} #原值是这样的
{'p': 23} #只是修改了临时对象
{'p': 42} #绑定名字后,达到修改的目的