Python implementations modify object instances through shelve

  • 2020-04-02 14:11:23
  • OfStack

This article is an example of a python implementation that USES shelve to modify objects.

The specific implementation method is as follows:


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 # No, this is just a temporary object 
print she['p']


a = she['p']# Give the temporary object a name 
a['p'] = 42
she['p'] = a
print she['p']

The example test environment in this article is Python2.7.6

The results of the program are as follows:


p {'p': 23}
a {'a': 23}
m {'m': 23}
s {'s': 23}
{'p': 23}# The original value looks like this 
{'p': 23}# Only temporary objects are modified 
{'p': 42}# After binding the name, to achieve the purpose of modification 

The example code and the results are annotated to help you understand what they mean. I hope this article has helped you with your Python programming.


Related articles: