Python copy object (deep copy and shallow copy), pythondeepcopy

Source: Internet
Author: User

Python copy object (deep copy and shallow copy), pythondeepcopy

Http://www.jb51.net/article/15714.htm

 

1. copy. copy: only the parent object is copied, and the internal sub-objects of the object are not copied.
2. copy. deepcopy: Deep copy object and its sub-objects

 

A good example:

1 import copy 2 a = [1, 2, 3, 4, ['A', 'B'] # original object 3 4 B = a # assign a value, upload object reference 5 c = copy. copy (a) # object copy, shortest copy 6 d = copy. deepcopy (a) # object copy, deep copy 7 8. append (5) # modify object a 9 a [4]. append ('C') # modify ['A', 'B'] array object 10 11 print ('a = ', a) in object) 12 print ('B =', B) 13 print ('C = ', c) 14 print ('d =', d) 15 16 # running result 17 '''18 a = [1, 2, 3, 4, ['A', 'B', 'C'], 5] 19 B = [1, 2, 3, 4, ['A', 'B', 'C'], 5] 20 c = [1, 2, 3, 4, ['A', 'B', 'C'] 21 d = [1, 2, 3, 4, ['A ', 'B'] 22 '''

 

Related Article

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.