Python Learning-assignment, shallow copy, and deep copy

Source: Internet
Author: User
Tags shallow copy

Python Copy:

In the Python language, there are two forms of shallow copy and deep copy, which is shadow copy in the Official document. When copying simple objects (object), there is no difference between the two, as shown in the following code:

1 #shallow copy and deep copy comparison of simple objects2 ImportCopy3List1 = [1,2,3,4,5]4 #Shallow Copy5Shadow_copy_list1 =copy.copy (List1)6 Print(List1)#[1, 2, 3, 4, 5]7 Print(SHADOW_COPY_LIST1)#[1, 2, 3, 4, 5]8 #Modify the original object element: Add ' 6 ' at the end of the list9List1.append (6)Ten Print(List1)#[1, 2, 3, 4, 5, 6] One Print(SHADOW_COPY_LIST1)#[1, 2, 3, 4, 5] A Print(ID (list1))#地址:2483079696328 - Print(ID (SHADOW_COPY_LIST1))#地址:2483079778504 -  the #Deep Copy -Deep_copy_list1 =copy.deepcopy (List1) - Print(List1)#[1, 2, 3, 4, 5] - Print(DEEP_COPY_LIST1)#[1, 2, 3, 4, 5] + #Modify the original object element: Add ' 6 ' at the end of the list -List1.append (6) + Print(List1)#[1, 2, 3, 4, 5, 6] A Print(DEEP_COPY_LIST1)#[1, 2, 3, 4, 5] at  - Print(ID (list1))#地址:1927356387400 - Print(ID (DEEP_COPY_LIST1))#地址:1927356387592

From the Simple object's shallow copy and deep copy you can see that the elements in the original list are modified, and the elements inside the Copy object do not change.

For shallow copy and deep copy, there are differences in copy of complex objects, as shown in the following program:

1 #shallow copy and deep copy comparison of complex objects2 ImportCopy3List1 = [1,2,3,[4,5]]4 #Shallow Copy5Shadow_copy_list1 =copy.copy (List1)6List1[3][0] = 77 Print(List1)#[1, 2, 3, [7, 5]]8 Print(SHADOW_COPY_LIST1)#[1, 2, 3, [7, 5]]9 Print(ID (list1))#地址:2201242341640Ten Print(ID (SHADOW_COPY_LIST1))#地址:2201242342920 One  ALIST1[3][0] = 4#Restore the list to the initial list - #Deep Copy -Deep_copy_list1 =copy.deepcopy (List1) theList1[3][0] = 7 - Print(List1)#[1, 2, 3, [7, 5]] - Print(DEEP_COPY_LIST1)#[1, 2, 3, [4, 5]] - Print(ID (list1))#地址:2201242341640 + Print(ID (DEEP_COPY_LIST1))#地址:2201242342856

From the shallow copy and deep copy of complex objects, it can be seen that when shallow copy, if the child object elements of complex objects are modified, the original list and copy of the list of sub-object elements will change , but when the deep copy, if the complex object's sub-object elements have changed, All elements of the copy object are not changed. What is the reason for this, because a shallow copy replicates only the properties of one layer of objects, while a deep copy recursively replicates all levels.

1. Copy.copy a shallow copy copies only the parent object and does not copy the inner child objects of the object.

2. Copy.deepcopy deep copy Copy object and its sub-object

(Deep copy)

Deep copy creates a new object to reassign the memory address to produce exactly the exact same content in memory.

(Light copy)

Shallow copy also re-assigns a new memory address to the copied object. However, in shallow copy, copying a child object is equivalent to copying a reference to a child object, at which point the child object belongs to the public object, and if the object has references to other objects, the contents of the child object are changed if the child object is modified.

Summarize:

    1. In a simple object, whether it is a shallow copy or a deep copy, the changes in the elements within the original object do not cause changes to the elements inside the copied object.
    2. In a complex object, the child object element of the original object changes, and the sub-object element inside the shallow copy object also changes, at which point the sub-object is equivalent to the image file and has an effect on the object referencing it, whereas in deep copy, because the deep copy is a copy of all the elements, So the change of the face object element in the original object will not result in the change of elements inside the copied object.

Python Assignment:

Assignment of simple objects, shallow copy and deep copy comparison:

1 #assignment of simple objects, shallow copy and deep copy comparison2 ImportCopy3List1 = [1,2,3,4,5]4New_list1 =List15Shadow_copy_list1 =copy.copy (List1)6Deep_copy_list1 =copy.deepcopy (List1)7 #Modify the original object element: Add ' 6 ' at the end of the list8List1.append (6)9 #Original listTen Print(List1)#[1, 2, 3, 4, 5, 6] One Print(ID (list1))#Address: 1913255661704 A #Assignment List - Print(NEW_LIST1)#[1, 2, 3, 4, 5, 6] - Print(ID (NEW_LIST1))#Address: 1913255661704 the #Shallow Copy list - Print(SHADOW_COPY_LIST1)#[1, 2, 3, 4, 5] - Print(ID (SHADOW_COPY_LIST1))#Address: 1913255661896 - #Deep Copy List + Print(DEEP_COPY_LIST1)#[1, 2, 3, 4, 5] - Print(ID (DEEP_COPY_LIST1))#Address: 1913255663176

As you can see from the running results of the program, the Wakahara list elements change in a simple object, and the new list elements that are assigned are also changed, and the list elements of both shallow copy and deep copy do not change. It can be thought that the assignment is to put two labels on the original object, such as the List1 tag in the program and NEW_LIST1 two tags, the same address in memory, the change of the elements in the list, the two list will change. Copying with copy is different, both shallow copy and deep copy, which will re-allocate an address in memory, so that changes to the elements in the original list do not change in the copied list.

Assignment of complex objects, shallow copy and deep copy comparison:

1 #assignment of complex objects, shallow copy and deep copy comparison2 ImportCopy3List1 = [1,2,3,[4,5]]4New_list1 =List15Shadow_copy_list1 =copy.copy (List1)6Deep_copy_list1 =copy.deepcopy (List1)7 #Modify the original object element: Change the ' 4 ' in the 4th element in the list to ' 7 '8List1[3][0] = 79 #Original listTen Print(List1)#[1, 2, 3, [7, 5]] One Print(ID (list1))#Address: 1975516434760 A #Assignment List - Print(NEW_LIST1)#[1, 2, 3, [7, 5]] - Print(ID (NEW_LIST1))#Address: 1975516434760 the #Shallow Copy list - Print(SHADOW_COPY_LIST1)#[1, 2, 3, [7, 5]] - Print(ID (SHADOW_COPY_LIST1))#Address: 1975516436040 - #Deep Copy List + Print(DEEP_COPY_LIST1)#[1, 2, 3, [4, 5]] - Print(ID (DEEP_COPY_LIST1))#Address: 1975516435976

As you can see from the running results of a program, when you modify a child object element of a complex object, the sub-objects in the list of assigned values and the shallow copy are also changed, because when the child object in the complex object is a public object, when shallow copy, the new list is also the child object in the referenced original list. While deep copy is not the same, the deep copy is recursive, copying all the layers of the list, so the changes in the elements of the original list will not cause changes in the sub-objects in the list of deep copy.

Beginners, if there is an incorrect analysis of the place, welcome to exchange discussions.

Python Learning-assignment, shallow copy, and deep 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.