python: the difference copy () and deepcopy () of

Transfer: HTTPS: // blog.csdn.net / qq_32907349 / Article This article was / the Details / 52190796`


				版权声明:本文为博主原创文章,欢迎随时转载,转载时请务必注明出处,同时欢迎广大朋友指点评论。					https://blog.csdn.net/qq_32907349/article/details/52190796				</div>
							<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
							            <div id="content_views" class="markdown_views">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<p>最近在实习,boss给布置了一个python的小任务,学习过程中发现copy()和deepcopy()这对好基友实在是有点过分,搞的博主就有点傻傻分不清啊,但是呢本着一探到底的精神,还是要查资料搞清楚这对好基友的区别。</p>

Actually, the distinction between the copy () and deepcopy () must be related to the storage of data for the python.

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

<p>复杂的 object, 如 list 中套着 list 的情况,shallow copy 中的 子list,并未从原 object 真的「独立」出来。也就是说,如果你改变原 object 的子 list 中的一个元素,你的 copy 就会跟着一起变。这跟我们直觉上对「复制」的理解不同。</p>

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

>>> import copy
>>> origin = [1, 2, [3, 4]]
#origin 里边有三个元素:1, 2,[3, 4]
>>> cop1 = copy.copy(origin)
>>> cop2 = copy.deepcopy(origin)
>>> cop1 == cop2
True
>>> cop1 is cop2
False 
#cop1 和 cop2 看上去相同,但已不再是同一个object
>>> origin[2][0] = "hey!" 
>>> origin
[1, 2, ['hey!', 4]]
>>> cop1
[1, 2, ['hey!', 4]]
>>> cop2
[1, 2, [3, 4]]
#把origin内的子list [3, 4] 改掉了一个元素,观察 cop1 和 cop2
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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:

>> a = [1, 2, 3]
>>> b = a
>>> a = [4, 5, 6] //赋新的值给 a
>>> a
[4, 5, 6]
>>> b
[1, 2, 3]
# a 的值改变后,b 并没有随着 a

>>> a = [1, 2, 3]
>>> b = a
>>> a[0], a[1], a[2] = 4, 5, 6 //改变原来 list 中的元素
>>> a
[4, 5, 6]
>>> b
[4, 5, 6]
# a 的值改变后,b 随着 a 变了
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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.
Write pictures described here
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.

Write pictures described here
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 do know this will ask, for a complex object shallow copy, in the end what happened at the copy time?
Look at a piece of code:

>>> import copy
>>> origin = [1, 2, [3, 4]]
#origin 里边有三个元素:1, 2,[3, 4]
>>> cop1 = copy.copy(origin)
>>> cop2 = copy.deepcopy(origin)
>>> cop1 == cop2
True
>>> cop1 is cop2
False 
#cop1 和 cop2 看上去相同,但已不再是同一个object
>>> origin[2][0] = "hey!" 
>>> origin
[1, 2, ['hey!', 4]]
>>> cop1
[1, 2, ['hey!', 4]]
>>> cop2
[1, 2, [3, 4]]
#把origin内的子list [3, 4] 改掉了一个元素,观察 cop1 和 cop2
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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:
Write pictures described here

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:
Write pictures described here

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

`

Guess you like

Origin blog.csdn.net/w_xiaowen/article/details/88975392