Deep copy and shallow copy of Python objects

Source: Internet
Author: User
This article mainly introduces the deep copy and shallow copy details of Python objects. This article is excerpted from the Python core programming 2 book and summarized in a more incisive manner, if you need it, you can refer to the content in this article on "Python core programming 2". it is very useful and can be written for your reference!

Shortest copy

First, we use two methods to copy objects, one is slicing and the other is the factory method. Then, use the id function to check their identifiers.

The code is as follows:


# Encoding = UTF-8

Obj = ['name', ['age', 18]
A = obj [:]
B = list (obj)
For x in obj, a, B:
Print id (x)

35217032
35227912
29943304



Their IDs are different. according to the normal judgment, the objects with three different IDs should be independent. Let's change their names first.

The code is as follows:


# Encoding = UTF-8

Obj = ['name', ['age', 18]
A = obj [:]
B = list (obj)
For x in obj, a, B:
Print id (x)

A [0] = 'lisi'
B [0] = 'hangsan'

Print
Print B

35217032
35227912
33547784
['Lisi', ['age', 18]
['Hangsan', ['age', 18]


Object a and object B have different names respectively. let's take a look at how to change the age of object.

The code is as follows:


# Encoding = UTF-8

Obj = ['name', ['age', 18]
A = obj [:]
B = list (obj)
For x in obj, a, B:
Print id (x)

A [0] = 'lisi'
B [0] = 'hangsan'

Print
Print B

A [1] [1] = 25

Print
Print B

35217032
35227912
29943304
['Lisi', ['age', 18]
['Hangsan', ['age', 18]
['Lisi', ['age', 25]
['Hangsan', ['age', 25]


Careful friends should see that changing element a [0] and Element B [0] do not affect each other, why does changing the elements of a [1] [1] affect the elements of B [1] [1?
To solve this problem, you only need to first understand the deep copy and shallow copy. In the above example, both a and B are created from the shallow copy of the obj object. The first element in obj is that the string belongs to the immutable type, and the second element is that the list belongs to the variable type. Therefore, when we copy an object, the string is displayed as a copy and a new string is created, while the list is just a copy reference. Therefore, changing the list element affects all referenced objects. You can see from the following id values

The code is as follows:


# Encoding = UTF-8

Obj = ['name', ['age', 18]
A = obj [:]
B = list (obj)

For x in obj, a, B:
Print id (x [0]), id (x [1])
Print

A [0] = 'lisi'
B [0] = 'hangsan'

For x in obj, a, B:
Print id (x [0]), id (x [1])
Print

A [1] [1] = 25
B [1] [1] = 30

For x in obj, a, B:
Print id (x [0]), id (x [1])
Print

32564088 34496008
32564088 34496008
32564088 34496008

32564088 34496008
34574704 34496008
33970672 34496008

32564088 34496008
34574704 34496008
33970672 34496008


When copying an object, we can see that the IDs of all elements are always. we changed the first string element of object a and object B, because the string is an immutable object, so after the change, it is equal to the new one, so the id of the first string element of a and B is inconsistent. The second element of a and B is a list variable object. therefore, no matter you modify any id value, it represents a pointer and affects the values of other referenced objects.
Therefore, modifying the age of object a affects the age value of object B, or modifying the age value of object B also affects the age value of object a, including Object obj.

Deep copy

The above are all light copies, so we want to copy objects independently and do not affect other values during modification. this is called deep copy. To implement deep copy, we need to reference a copy module. the copy module has two functions available, one is copy shortest, and the other is deepcopy.

The code is as follows:


# Encoding = UTF-8
Import copy
Obj = ['name', ['age', 18]
A = copy. deepcopy (obj)
B = copy. deepcopy (obj)

For x in a, B:
Print id (x [0]), id (x [1])
Print

A [1] [1] = 25
B [1] [1] = 30

Print
Print B

33612664 35477256
33612664 35477640

['Name', ['age', 25]
['Name', ['age', 30]


After deep copy is used, the IDs of list elements are inconsistent, indicating independent objects. modifying the values of any list element does not affect other objects.

Note the following:
First, non-container types (such as numbers, strings, and other "yard" objects, such as code, type, and range objects) are not copied, the shallow copy is completed using the full slicing operation.

Second, if the metachild variable only contains an atomic object, its deep copy will not be performed.
Let's change the above example to the ancestor, and then try using the deep copy.

The code is as follows:


# Encoding = UTF-8
Import copy
Obj = ['name', ('age', 18)]
A = copy. deepcopy (obj)
B = copy. deepcopy (obj)

For x in a, B:
Print id (x), id (x [1])
Print

34703752 34693000
34756616 34693000

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.