Python deep copy and shallow copy------copy module

Source: Internet
Author: User
Tags shallow copy

Module Interpretation:

Shallow copy: x = copy.copy (y)
Deep copy: x = copy.deepcopy (y)
(Note: module-specific exceptions, copy.) Error)

The difference between a deep copy and a shallow copy is that when you have a mixed object, you include other sub-objects in an object, such as a value in a dictionary is a list, at this point:
Shallow copy, no sub-objects are copied, so the original data changes and the sub-objects change.
Deep copy, which contains copies of the child objects in the copied object, that is, the original data changes, the sub-object does not change.

As an example:
info = {
' Name ': ' Webber ',
' Job ': ' IT ',
' Age ': 24,
' Girlfriend ': [' A ', ' B ', ' C ']
}

New_info = copy.copy (info) #浅copy
new_info[' sex '] = ' male '

print ' info: ', info
print ' New_info ', new_info

Output:
Info: {' girlfriend ': [' A ', ' B ', ' C '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}
New_info {' girlfriend ': [' A ', ' B ', ' C '], ' age ': +, ' sex ': ' Male ', ' job ': ' IT ', ' name ': ' Webber '}

-------------------------------------------------------------------------------------------

New_info = copy.copy (info) #浅copy
new_info[' girlfriend '].append (' dddd ')

Output:
Info: {' girlfriend ': [' A ', ' B ', ' C ', ' dddd '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}
New_info {' girlfriend ': [' A ', ' B ', ' C ', ' dddd '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}

-------------------------------------------------------------------------------------------

New_info = copy.deepcopy (info) #深copy
new_info[' sex '] = ' male '

Output:
Info: {' girlfriend ': [' A ', ' B ', ' C '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}
New_info {' girlfriend ': [' A ', ' B ', ' C '], ' age ': +, ' sex ': ' Male ', ' job ': ' IT ', ' name ': ' Webber '}

-------------------------------------------------------------------------------------------

New_info = copy.deepcopy (info) #深copy
new_info[' girlfriend '].append (' dddd ')

Output:
Info: {' girlfriend ': [' A ', ' B ', ' C '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}
New_info {' girlfriend ': [' A ', ' B ', ' C ', ' dddd '], ' age ': ' ' job ': ' IT ', ' name ': ' Webber '}

--------------------------------------------------------------------------------------------

It is found here that when adding sex information to a dictionary, both the deep copy and the shallow copy do not affect each other, but there is a difference in adding information to the dictionary's sub-object list, and the shallow copy object has been modified, while the deep copy does not, update the new_info information, The information in the info dictionary has not changed.

Here also to distinguish between shallow copy and direct assignment :
The direct assignment is to pass the object's reference, that is, the original data changes, the new assignment object data will also change,

Follow the example above:
New_info = info
new_info[' sex '] = ' male '

Output:
Info: {' girlfriend ': [' A ', ' B ', ' C '], ' age ': ' Job ': ' IT ', ' name ': ' Webber ', ' sex ': ' Male '}
New_info {' girlfriend ': [' A ', ' B ', ' C '], ' age ': ' Job ': ' IT ', ' name ': ' Webber ', ' sex ': ' Male '} #完全相同

Whoever modifies, the other changes, because they point to the same memory space.

Another detailed example:http://www.cnblogs.com/xueli/p/4952063.html

=============================================================

In addition, the module usage mentions two points of note that may be encountered in deep copy, while the shallow copy does not exist:

1, deep copy recursive all sub-objects are copied, there is a recursive loop, may cause a lack of performance resources, use should be careful.

2, because the deep copy of an object to copy the "all", then when the deep copy of an important object, if it contains administrative permissions of the data structure, this may be to share this information to the object does not have administrative rights.

Python deep copy and shallow copy------copy module

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.