Pytoh casual copy and deep copy

Source: Internet
Author: User

In python, assigning values to variables is actually a shallow copy action. The object on the right of the assignment number is assigned to the variable on the left of the assignment number.

 
S = "abcdefg"

As mentioned aboveCodeCreate a String object with the content abcdefg, and then assign the object reference to variable S. Similarly, if S is assigned to another string variable S1, the reference is also assigned to S1. Therefore, modifications to S1, because they point to the same memory address.

For simple data types, there is no difference between shallow copy and deep copy. However, for complex data types, such as lists, dictionaries, classes, or other custom nested data types, there are still differences between the shortest copy and the deep copy, the following describes the dictionary type.

 

Import copyd1 = {"name": "Chris", "Account": {"RMB": 1000, "USD": 12345} D2 = copy. copy (D1) D3 = dict (D1) D4 = d1print [ID (x) for X in D1, D2, D3, d4] D2 ["name"] = "Martin" # modify the name value d2 ["Account"] ["RMB"] = 2000 # modify the RMB account amount in D2 print [X for X in D1, d2, D4] # output D1, D2, D4 content. D4 and D1 content are the same, modifying the RMB account amount of D2 will affect the RMB account amount in D1 D3 ["name"] = "Vivian" # modifying the name value D3 ["Account"] ["RMB"] = 3000 # modify the RMB account amount in D3 print [X for X in D1, d3, D4] # The content of D1, D3, D4 is the same as that of D1. Modifying the RMB account amount of D3 will affect the RMB account amount of D1 D3 = copy. deepcopy (D1) d3 ["name"] = "Vivian" # modify the name value D3 ["Account"] ["RMB"] = 6000 # modify the RMB account amount in D3 print [X X in D1, d3, D4] # The content of D1, D3, D4 is the same as that of D1. Modifying the RMB account amount of D3 does not affect the RMB account amount in D1.

The output result is

[13744000,137 45872, 13746016,137 44000] [{'account': {'RMB': 2000, 'usd': 12345}, 'name': 'chris '}, {'account': {'RMB': 2000, 'usd': 12345}, 'name': 'martin '}, {'account': {' RMB ': 2000, 'usd': 12345}, 'name': 'chris '}] [{'account': {' RMB ': 3000, 'usd': 12345}, 'name ': 'chris '}, {'account': {' RMB ': 3000, 'usd': 12345}, 'name': 'vivianc'}, {'account ': {'RMB': 3000, 'usd': 12345}, 'name': 'chris '}] [{'account': {' RMB ': 3000, 'usd ': 12345}, 'name': 'chris '}, {'account': {' RMB ': 6000, 'usd': 12345}, 'name': 'vivianc '}, {'account': {'RMB': 3000, 'usd': 12345}, 'name': 'chris '}]

First, we define a dictionary variable D1 in row 3rd, and use the copy method (Shortest copy) and factory method (dict) in the copy module in rows 4th to six) and the assignment numbers are assigned to D2, D3, and D4.

The memory address of the four variables is output in the 8th statement. It can be found that D1 and D4 point to the same address, which confirms that the value assignment number is used to assign values to the reference.

D2 and D3 have their own memory addresses and are different from D1. This shows that the copy function and the factory method (dict) will open up new memory space, copy the content on the right of the assignment number to the new memory address.

From 10th to 15 lines of code, you can modify the content of the variable D2 and D3 and the content of its sub-object (account is their sub-object). You can find that the value of key as name in D2 and D3 has changed, the value in D1 is not affected. However, when we change the key in D2 and D3 to the RMB value in the account, the RMB value of the variable D1 also changes,This indicates that the shortest copy operation only copies the content of the parent object, and its sub-object is still referenced. That is to say, the account objects in D1, D2, and D3 point to the same memory address, so whether we modify the account in D2 or D3, it will affect the account value in D1.To make these three variables affect each other, we need to use the deepcopy method in the copy module.

Lines 17th to 20 use the deepcopy method. Modifications to the D3 sub-objects do not affect the D1 sub-objects.

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.