Python depth Copy

Source: Internet
Author: User
Tags shallow copy

1. For the assignment of characters and numbers, deep copy and shallow copy

Import Copy
S1 = "ABCDEFG"
S2 = S1
Print (ID (S1))
Print (ID (s2))
Results:

24266496
24266496

Conclusion: For assignment operations, memory addresses are consistent.

Import Copy
S1 = "ABCDEFG"
S2 = copy.copy (S1)
Print (ID (S1))
Print (ID (s2))
Results:

5129984
5129984

Conclusion: For shallow copies, memory addresses are consistent.

Import Copy
S1 = "ABCDEFG"
S2 = copy.deepcopy (S1)
Print (ID (S1))
Print (ID (s2))
Results:

24069888
24069888

Conclusion: For deep copies, memory addresses are also consistent

2. For list, Ganso and dictionary assignment, deep copy and shallow copy

Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2":(' v21 ', ' v22 ', ' v23 '), "K3": "V31"}
N2 = N1
Print (ID (N1))
Print (ID (n2))
Print (ID (n1[' k1 '][0]))
Print (ID (n2[' k1 '][0]))
Results:

24617448
24617448
24594208
24594208

Conclusion: For assignment operation, N2 and N1 are the same pointing relation

Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2": [' v21 ', ' v22 ', ' v23 '], "K3": "V31"}
N2 = Copy.copy (n1)
Print (ID (N1))
Print (ID (n2))

Print (ID (n1[' k1 '))
Print (ID (n2[' k1 '))
Results:

30712296
30965472
31193048
31193048

Conclusion: For a shallow copy, only the first layer is a new memory area in memory to save the pointing relationship

Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2": [' v21 ', ' v22 ', ' v23 '], "K3": "V31"}
N2 = Copy.deepcopy (n1)
Print (ID (N1))
Print (ID (n2))

Print (ID (n1[' k1 '))
Print (ID (n2[' k1 '))

Print (ID (n1[' k1 '][0]))
Print (ID (n2[' k1 '][0]))
Results:

21144040
21147600
24639488
24638088
21120800
21120800

Conclusion: For deep copy, copy deep internal structure

Applications for deep-shade copies:

Import Copy
DIC = {
"CPU": [80,],
"Mem": [80,],
"Disk": [80,]
}

Print (DIC)

New_dic = Copy.deepcopy (DIC)
new_dic["CPU"][0] = 50
Print (DIC)
Print (New_dic)

Results:

{' Disk ': [+], ' mem ': [+], ' CPU ': [80]}
{' Disk ': [+], ' mem ': [+], ' CPU ': [80]}
{' Disk ': [+], ' mem ': [+], ' CPU ': [50]}

Conclusion: The original valve value is not modified, the newly added server threshold is reset to 50. If a shallow copy is used, the new threshold value will be changed.

Python depth Copy

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.