Home > Backend Development > Python Tutorial > python实现通过shelve修改对象实例

python实现通过shelve修改对象实例

WBOY
Release: 2016-06-16 08:41:46
Original
1024 people have browsed it

本文实例讲述了python实现通过shelve修改对象的方法,分享给大家供大家参考。

具体实现方法如下:

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']

Copy after login

本文实例测试环境为Python2.7.6

程序运行结果如下:

p {'p': 23}
a {'a': 23}
m {'m': 23}
s {'s': 23}
{'p': 23}#原值是这样的
{'p': 23}#只是修改了临时对象
{'p': 42}#绑定名字后,达到修改的目的

Copy after login

实例代码及运行结果均配有较为详尽的注释,帮助大家理解其含义。希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template