Python copy module with deep copy, shallow copy

Source: Internet
Author: User

Copy Module

Use the Copy module to implement the replication functionality.

The copy method is divided into shallow copy and deep copy:

Shallow copy,

Copy.copy (object)

The memory address is copied, and the target object and source object point to the same memory memory space.

Deep Copy,

Copy.deepcopy (object)

The objects generated for replication also request a separate memory space, where the memory addresses are allocated autonomously.

When replication is complete, the destination object and the source object are stored in the same data, but the memory addresses are different.

Two objects do not interfere with each other.

Cases:

>>> jack = [' Jack ', [' age ', ' 20 ']
>>> Import Copy


Deep copy
>>> James = Copy.deepcopy (Jack)

>>> james[0] = ' James '

>>> Print Jack, James
[' Jack ', [' age ', ' 20 '] [' James ', [' age ', ' 20 ']
>>> James[1][1] = 18
>>> Print Jack, James
[' Jack ', [' age ', ' 20 '] [' James ', [' age ', 18]]

At this time they will not affect each other. Print out each person's internal element each ID:

>>> [ID (x) for x in Jack]
[139132064, 3073507244L]
>>> [ID (x) for x in James]
[139141632, 139157548]

Their internal elements also point to different objects.

Shallow copy

>>> love = Copy.copy (Jack)

>>> love[0] = ' love '

>>> print Jack, Love
[' Jack ', [' age ', ' 20 '] [' Love ', [' age ', ' 20 ']
>>> James[1][1] = 18
>>> print Jack, Love
[' Jack ', [' age ', ' 18 '] [' Love ', [' age ', 18]]

>>> jack.append ([' Sex ', ' man '])

>>> Print Jack

[' Jack ', [' age ', ' + '], [' Sex ', ' man ']

>>> Print Love

[' Jack ', [' age ', ' 20 ']

The copy () method copies the memory value of the source object variable to the target object, and two objects share the same store.

A shallow copy creates a new object, but does not create a data area for the new object.

Because the first element string is an immutable type, the change generates a new object, and the second element list is a mutable type, so the original object is modified.

After a shallow copy, the target object is not affected by adding elements to the source object.


Using the slice and list method to implement a shallow copy

>>> jack = [' Jack ', [' age ', 20]]
>>> James = jack[:]
>>> Anny = List (Jack)

Observe the ID value of the three, the three are different objects.

>>> Print ID (jack), ID (TOM), ID (anny)

144846988 144977164 144977388

Modify the name in each value and observe again

>>> james[0] = ' James '
>>> anny[0] = ' Anny '
>>> print Jack, James, Anny

[' Jack ', [' age ', 20]] [' James ', [' age ', 20]] [' Anny ', [' age ', 20]]

Everything looks normal, and when you rename James and Anny, you re-create a ' James ' and ' Anny ' object, replacing the old ' Jack ' object.

But Anny is only 18 years old and Anny to define age.

>>> Anny[1][1] = 18
>>> print Jack, James, Anny
[' Jack ', [' age ', 18]] [' James ', [' age ', 18]] [' Anny ', [' age ', 18]]

Oddly enough, Jack, James, and Anny have changed their age, turning 18. Jack, Tom, Anny they should all be different objects, how can they affect each other? Look at the inner elements of Jack,james,anny each element ID:

>>> [ID (x) for x in Jack]
[3073896320L, 3073777580L]
>>> [ID (x) for x in James]
[144870744, 3073777580L]
>>> [ID (x) for x in Anny]

[144977344, 3073777580L]

Because the first element of the list is an immutable type, the first element of the list that corresponds to the source object uses a new object.
But the second element of the list is a mutable type, and the modification does not produce a new object, so the result of the modification responds to the other.


A comprehensive example:

Import Copy

A = [1,2,3,[' A ', ' B ']]

b = A

c = Copy.copy (a)

D = Copy.deepcopy (a)

A.append (4)

A[3].append (' C ')

Print A,b,c,d

The output is:

[1,2,3,[' A ', ' B ', ' C '],4]  [1,2,3,[' A ', ' B ', ' C '],4] [1,2,3,[' A ', ' B ', ' C ']

[1,2,3,[' A ', ' B ']


Knot

    • Using the slice [:] operation, using a factory function (such as List/dir/set), using the copy () function in the Copy module to implement the copy is called a shallow copy, only the outermost object itself is copied, the internal elements are just a copy of a reference.

Copying using the Deepcopy method in copy is called a deep copy, and both the outer and inner elements copy the object itself, not the reference.


Note: Using the = sign only adds a layer of labels to the source object to generate the target object, which gets the value through the label of the source object.



This article is from the "Ink" blog, please be sure to keep this source http://jinyudong.blog.51cto.com/10990408/1916494

Python copy module with deep copy, 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.