Detailed explanation of the Python core programming in the light copy and deep copy, detailed explanation of the python core programming

Source: Internet
Author: User

Detailed explanation of the Python core programming in the light copy and deep copy, detailed explanation of the python core programming

I. Problem Introduction

First, let's take a look at the execution of the following code:

A = [1, 2, 3] print ('a = % s' % a) # a = [1, 2, 3] B = aprint ('B = % s' % B) # B = [1, 2, 3]. append (4) # print ('a = % s' % a) for a modification # a = [1, 2, 3, 4] print ('B = % s' % B) # B = [1, 2, 3, 4] B. append (5) # print ('a = % s' % a) on B # a = [1, 2, 3, 4, 5] print ('B = % s' % B) # B = [1, 2, 3, 4, 5]

The code above is relatively simple. It defines a variable a, which is a list of values [1, 2, 3]. a simple assignment statement B = a defines variable B, it is also a list of values [1, 2, 3.

Question: If variable a is modified at this time, will it affect variable B? If variable B is modified, will it affect?

From the code running result, we can see that, whether it is modifying B or modifying a (note that this modification method uses append to directly modify the original list, rather than re-assigning values ), both parties have an impact.

Of course, this is actually a good understanding. Variable a points to the address values of the list [1, 2, 3]. When the value is assigned using =, the value of B also points to the address value of the list [1, 2, 3. In python, you can use the id (variable) method to view the address value. Let's check the address value of the and B variables to see if they are equal:

# Note: This value is different on different machines, but as long as the address values of the two variables a and B are the same, the problem is indicated by print (id ()) #4439402312 print (id (B) #4439402312

The principles are as follows:

Therefore, as long as the list on address value: 4439402312 is modified, a and B will change. (Note that the changes I mentioned here are made in the list on address value: 4439402312, instead of modifying variable, because there are two ways to modify variable a, this article will explain why not to modify variable ). Therefore, the following concepts are introduced:

This method is to assign a copy value to another variable (that is, copy the address value), which is called a shortest copy.

Ii. How to perform deep copy

The method for implementing deep copy in python is very simple. You only need to introduce the copy module and call the deepcopy () method in it. The sample code is as follows:

import copya = [1, 2, 3]b = copy.deepcopy(a)print('a = %s' % a) # a = [1, 2, 3]print('b = %s' % b) # b = [1, 2, 3]b.append(4)print('a = %s' % a) # a = [1, 2, 3]print('b = %s' % b) # b = [1, 2, 3, 4]

From the code execution, we have implemented deep copy. Now let's take a look at the address values of the two variables:

print(id(a)) # 4321416008print(id(b)) # 4321416200

It is really different. Let's look at the principle of deep copy through a diagram:

Iii. copy Module Method Introduction

From the implementation process of deep copy, we know that the copy module also uses the deepcopy () method in it. Next we will introduce the copy () and deepcopy () methods in the copy module.

First, we will introduce the deepcopy () method we have used. The official documentation is as follows:

This method is described in the following document:

1. The returned value is a deep copy of this object.

2. If an error occurs during the copy operation, a copy. err exception is reported.

3. There are two problems. The first is that if a recursive object is generated, it will be copied recursively. The second is that it will be copied recursively, resulting in excessive copies.

4. The difference between the two copy methods is that the object is relatively referenced.

The first two points are well understood. For the third point, we use the code to explain:

Import copya = [1, 2, 3] B = [3, 4, 5] c = [a, B] # list nesting d = copy. deepcopy (c) print ('C = % s' % c) # c = [[1, 2, 3], [3, 4, 5] print ('d = % s '% d) # d = [[1, 2, 3], [3, 4, 5] c. append (4) print ('C = % s' % c) # c = [[1, 2, 3], [3, 4, 5], 4] print ('d = % s' % d) # d = [[1, 2, 3], [3, 4, 5] c [0]. append (4) # equivalent to. append (4) print ('C = % s' % c) # c = [[1, 2, 3, 4], [3, 4, 5], 4] print ('d = % s' % d) # d = [[1, 2, 3], [3, 4, 5] #. append (4) # print ('C = % s' % c) # a = [1, 2, 3] # print ('d = % s' % d) # B = [1, 2, 3] print (id (c) #4314188040 print (id (d) #4314187976 print (id (c [0]) #4314186568 print (id (d [0]) #4314187912 print (id (a) #4314186568 print (id (B) #4314186760

According to the code, we can see that when there is a nested object, that is, the recursive object mentioned in the document, we can see from the results that the nested object will be recursively copied in depth. That is, if c contains a, not only will c be deeply copied, but a will also be deeply copied. The principles are as follows:

Next let's take a look at the copy () method:

The official document explains this very easily. It returns a shortest copy of the object. But in fact, it will make a deep copy of the outermost layer. If there are multiple layers, the second layer will be used for the shortest copy. The sample code is as follows:

Import copya = [1, 2, 3] B = [3, 4, 5] c = [a, B] # list nesting d = copy. copy (c) print ('C = % s' % c) # c = [1, 2, 3], [3, 4, 5] print ('d = % s '% d) # d = [[1, 2, 3], [3, 4, 5] c. append (4) print ('C = % s' % c) # c = [[1, 2, 3], [3, 4, 5], 4] print ('d = % s' % d) # d = [[1, 2, 3], [3, 4, 5] not changed, it indicates that the outer layer is a deep copy of c [0]. append (4) # equivalent to. append (4) print ('C = % s' % c) # c = [[1, 2, 3, 4], [3, 4, 5], 4] print ('d = % s' % d) # d = [[1, 2, 3, 4], [3, 4, 5] changed, indicates that the inner layer is a shallow copy #. append (4) # print ('C = % s' % c) # c = [[1, 2, 3, 4], [3, 4, 5], 4] # print ('d = % s' % d) # d = [[1, 2, 3, 4], [3, 4, 5] changed, it indicates that the inner layer is a light copy print (id (c) #4322576648 print (id (d) #4322576584 d and c address are different, the outer layer is a deep copy print (id (c [0]) #4322575176 print (id (d [0]) #4322575176 c [0] And d [0] address are the same, further indicating that the inner layer is a light copy print (id (a) #4322575176 print (id (B) #4322575368

[Note] for the copy () method, there are special cases, such as the tuple type. The code example is as follows:

Import copya = [1, 2, 3] B = [3, 4, 5] c = (a, B) # change the list to a tuple d = copy. copy (c) print (id (c) #4303015752 print (id (d) #4303015752 d and c address are the same print (id (c [0]) #4322575176 print (id (d [0]) #4322575176 c [0] And d [0] address are the same, further indicating that the inner layer is a light copy

It can be seen that, even the outermost layer, it is also a shallow copy.

This is because the copy method has internal judgment. If the copy type of the outermost layer is immutable, the shortest is performed. Otherwise, the deep copy is performed.

At this point, we should further understand the concept of the shallow copy:

If one of all the elements in the object is a reference copy, it is defined as a shallow copy. (This definition is not an official definition, but for personal understanding)

4. A description of "modification"

As mentioned above, there are two ways to modify variables. The first is to modify the original object, and the second is to re-assign values. See the following code:

import copya = [1, 2, 3]b = aa = [3, 4, 5]print(a) # [3, 4, 5]print(b) # [1, 2, 3]

It is also a small copy, but it is found that B has not changed after a is modified.

During the modification, we can easily take it for granted to modify it by re-assigning the value, but in fact this modification method is problematic. When a is assigned a value again, it actually points a to another address area, and the original [1, 2, 3] The address area has not changed, so for B, what it points to has not changed.

This also explains why the deepcopy method in the previous document is only useful for referenced objects, because the simple type modification method is to assign a value again. Simply put, you can't directly call your own methods through simple variables, and you can only change the value by re-assigning them, so they all point to the new address.

The above is all the content of this article on the shortest copy and deep copy of Python core programming. You can write your learning experience in the following message area. Thank you for your support for the help house.

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.