Advanced Python 1: copy () and deepcopy () difference

                                                 

Distinction between copy () and DeepCopy () must be related to the python for data storage.

First, the direct conclusion:

- our ordinary sense of deep copy is the copy, is about to be copied objects completely and then copy it again as an independent new individual alone. So change the original to be copied object does not have an impact on the new object has been copied out. 
- shallow and does not produce a separate copy of the object exists in isolation, he just the original data block marked with a new label, so when wherein a label is changed, the data block will vary, other labels It will change. This unusual and replication on the meaning of our somewhat different.

For simple object, no difference with shallow copy and deep copy

Complex object, such as a list in the set list of the cases, the child list shallow copy of, not "independent" from the original object really. That is, if you change one element of the original object's child list, your copy will change along together. This is different from our understanding of the intuition of "copy" of.

It does not matter that we can not read the text look at the code:

We can see cop1, which is shallow copy followed origin changed. The cop2, which is deep copy has not changed.

Deep copy seem more in line with our definition of intuition "Copy" to: Once copied out, it should be independent of. If we want is a literal meaning of "copy", it can be directly deep_copy.

So why is there such a shallow copy "fake" copy exist? This is an interesting place.

data storage python

Python method to store a variable with other OOP languages ​​are different. It is not so much the value assigned to the variable, as it is to establish a reference to a variable to a specific value.

When a = something in Python be understood as a label affixed to something a. When re-assigned to a, as if to a label down from the original something, stick to other objects, create a new reference. This explains some of the strange situations that may be encountered in Python:

The above two code, the value of a are changed. Except that the first piece of code is assigned directly to the new value a (from [1, 2, 3] to [4, 5, 6]); and the second piece is, respectively, to each element of the list change.

The impact of b is different, do not let a b value is changed, the other has changed. How to use the top reason to explain this strange difference will it make?

The first [1, 2, 3] as an article. a = [1, 2, 3] is equivalent to the article on a label affixed. And b = a is affixed to the goods and b of a label.

The first case:

= a [4, 5, 6] is equivalent to a peel off the label from [1, 2, 3], attached to the [4, 5, 6].

In this process, [1, 2, 3] This article does not disappear. Throughout are affixed properly b [1, 2, 3], since the reference has not changed. B value of the natural constant.

The second case:

a [0], a [1], a [2] = 4, 5, 6 are directly change [1, 2, 3] the article itself. Put it inside each part are refitted a bit. After the completion of internal conversion, [1, 2, 3] itself becomes [4, 5, 6].

And in which this process, a and b are not moving, they also posted on that article. Thus NATURAL ab values ​​have become [4, 5, 6].

After I understand this will do to ask for a complex object shallow copy, in the end what happened at the copy time? 
Look at a piece of code:

Learned docker who will be no stranger to the concept of the mirror, we can apply the concept of mirrored copy it.

FIG conceptual follows: 

For a copy complex object's children will not be fully replicated, what is a child of complex objects it? For example, on child objects are complex objects nested sequences in sequence, dictionary nested sequence and so on. For child objects, python will use it as a public image stored, copied all of his have been treated as a reference, so that when one of the references cited another using a mirror when the mirror after mirror changes has been changed .

So look here origin [2], which is [3, 4] this list. According to the definition of the shallow copy, in cop1 [2] points to the same list [3, 4]. So, if we are here to change this list, it will lead to origin and cop1 while changing. This is why after the upper origin [2] [0] = "hey!", Cop1 also will become a [1, 2, [ 'hey!', 4]].

And deepcopy conceptual diagram below: 

deepcopy when each layer of a replication complex objects will separate out individual. 

This time origin [2] and cop2 [2] Although the values ​​are equal to [3, 4], but the list is not the same. That copy of our ordinary sense.

Reference article:

 http://iaman.actor/blog/2016/04/17/copy-in-python

Reprinted blog:

https://blog.csdn.net/qq_32907349/article/details/52190796

 

 

 

     

    Guess you like

    Origin www.cnblogs.com/lianhaifeng/p/11909039.html