Python deep copy and shallow copy

Source: Internet
Author: User

First, the definition

Shallow copy: Creates a new object that contains references to the objects contained in the original object. (If one of the objects is modified by the way it is referenced, the other one changes as well)

Deep copy: Creates a new object and recursively assigns the object it contains. (Modify one, the other one will not change)

Second, the application

Shallow copy: 1. Complete Sectioning method

2. Factory functions, such as List ()

3. Copy () function in the Copy module

Deep copy: 1. The Deep.deepcopy () function under the Copy module

Third, examples

1. Shallow copy

1 ImportCopy2 3List1 = [1, 2, 3, ['a','b','C']]4List2=list1.copy ()5 #Print (ID (list1)) # 17104524505046 #Print (ID (list2)) # 17104524517847 #Description List2 is a new space that is isolated in memory8 9 Print(LIST2)#[1, 2, 3, [' A ', ' B ', ' C ']Ten #list1[0]= ' A ' # modified the value of List1 outermost (first layer) One #print (list1) # [' A ', 2, 3, [' A ', ' B ', ' C ' ] A #print (LIST2) # [1, 2, 3, [' A ', ' B ', ' C ' ] - ## List2 The first layer of information is not affected by List1 - ## does not mean that the information in the inner layer is all copied.  the  - #list2[0]= ' A ' # modifies the value of the outermost (first layer) of the List2 - #print (List1) # [1, 2, 3, [' A ', ' B ', ' C ' ] - #print (list2) # [' A ', 2, 3, [' A ', ' B ', ' C ' ] + ## confirms that there is no interaction between List2 and List1 's first-level information . - ## This step also does not confirm whether the memory information is a deep copy +  A #list1[3][0]=1 # Modifying the value of the list1 inner layer at #print (List1) # [1, 2, 3, [1, ' B ', ' C '] - #print (LIST2) # [1, 2, 3, [1, ' B ', ' C '] - ## List1 The value of the inner layer changes, and the value of the list2 inner layer changes . - ## There may be some kind of connection between List1 and List2. -  - #List2[3][0]=1 # Modified the value of the list2 inner layer in #print (List1) # [1, 2, 3, [1, ' B ', ' C '] - #print (LIST2) # [1, 2, 3, [1, ' B ', ' C '] to ## confirms that there's a mutual influence between List1 and List2. + ## It's supposed to be list2. The memory address of the inner data information is the address of the inner data that points to the List1

2. Deep copy

1 ImportCopy2 3List1 = [1, 2, 3, ['a','b','C']]4List2=copy.deepcopy (List1)5 #modifies the outermost value whose result is consistent with the shallow copy6 7 #list1[3][0]=18 #print (List1) # [1, 2, 3, [1, ' B ', ' C ']9 #print (LIST2) # [1, 2, 3, [' A ', ' B ', ' C ' ]Ten ## List1 and List2 don't have a reciprocal effect. One  A #list2[3][0]=1 - #print (List1) # [1, 2, 3, [' A ', ' B ', ' C ' ] - #print (LIST2) # [1, 2, 3, [1, ' B ', ' C '] the ## List2 and List1 don't have a reciprocal effect. -  - #confirms that a deep copy is a recursive copy of all data information

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