Python implementation modifies object instances through shelve _python

Source: Internet
Author: User

This article describes the Python implementation through the shelve to modify the object of the method to share for everyone to reference.

The implementation methods are 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 # This modification is not possible, this just modifies a temporary object
print she[' P ']


a = she[' P '] #给临时对象绑定个名字
a[' p '] = "
she[' P '" = a
print she [' P ']

This article example test environment for Python2.7.6

The results of the program operation are as follows:

p {' P ':}
a {' A ':}
m {' m ':}
S {' s ':} {' P '
:} #原值是这样的 {'
P ':} #只是修改了临时对象 {' P ']: #绑定名字后, to achieve the purpose of the modification

Both the instance code and the running results are fully annotated to help you understand what it means. I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.