Python path-depth copy

Source: Internet
Author: User
Tags shallow copy

The depth copy usage comes from the copy module.

Import module: Import copy

Shallow copy: Copy.copy

Deep copy: Deepcopy

Literal understanding: A shallow copy refers to the first layer of data that copies only the data set, and the deep copy refers to all the layers of the copied data set. So for a single layer of data collection, the meaning of the deep copy is the same, such as strings, numbers, and just a layer of dictionaries, lists, Ganso, etc.

The meaning of the following data shades is the same:

' Beijing '  = [1,2,3,4 = {' name ': 'Beijing' ,'age': 20}

There is only one layer of the above data types, so the depth copy is the same.

To understand a deep copy from a memory address:

Shallow copy:

strings, Shades of numbers

For example, a deep copy of a number and a string simply points the index of the variable to the original memory address, such as in num,num1,num2 three variables, regardless of any one of the variables, just point it to another memory address, the other two variables will not change, the same string.

Therefore, for numbers and strings, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address.

A dark copy of a dictionary (list)

Assignment value:

Import= {'K1':'wu','K2 ': 123,'k3': ['Alex', 678  = N1

Shallow copy:

Import= {'K1':'wu','K2 ': 123,'k3': ['Alex', 678  = Copy.copy (N1)

Deep copy:

Import= {'K1':'wu','K2 ': 123,'k3': ['Alex', 678  = Copy.deepcopy (N1)

Application scenarios for deep-shade copies

For example, in the CMDB system, we define an alarm template call to all the server use, at this time there are a number of special applications of the server need to alarm parameters, we do not want to create a separate new template to add alarm parameters, and do not want to modify the default template to affect other machine alarm thresholds. At this point we need to do it with a deep copy. Examples are as follows:

Default Template:

Call = {    'CPU': +,    'mem':  ,    'disk':

The special template requirement at this time is to change the CPU alarm threshold to 75, without affecting the default template usage

The code is as follows:

#Default TemplateCall = {    'CPU': [80,],    'Mem': [80,],    'Disk': [80,]}#New TemplateNew_call =copy.deepcopy (call)#Modify the new templatenew_call['CPU'] = 75#view values for new and old templatesPrint('the new template is:%s'%(New_call))Print('The default template is:%s'%(call))#Printing results:The new template is: {'Mem': 80,'Disk': 80,'CPU': 75The default template is: {'Mem': 80,'Disk': 80,'CPU': 80}


#上面的代码显示我们只改了新的模版, and the default template was not modified, and we used copy instead of creating a new template alone.



Let's say we use a light copy to do the result:
#Default TemplateCall = {    'CPU': [80,],    'Mem': [80,],    'Disk': [80,]}#New TemplateNew_call =copy.deepcopy (call)#Modify the new templatenew_call['CPU'] = 75#view values for new and old templatesPrint('the new template is:%s'%(New_call))Print('The default template is:%s'%(call))#Printed results:The new template is: {'Mem': [80],'Disk': [80],'CPU': [75]} The default template is: {'Mem': [80],'Disk': [80],'CPU': [75]}#The default and new templates have been modified, which is obviously not the result we want.

Analysis reason: When deep copy python creates a new copy of all the data in the dictionary in memory, so if you modify the new template, the old template will not change. Instead, at shallow copy, Python simply creates a new copy of the outermost content in memory, and the second-level list of the dictionary is not created in memory, so you modify the new template and the default template is modified.

Note: Deep copy does not create new elements in the list in memory because the elements of the list in the previous example are numbers, thanks to Python's optimization of numbers and strings, that is, if the string or number is not modified, its memory location will never change until it is modified.



Python path-depth copy

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.