Python reference from the difference between copy and deepcopy in Python

Source: Internet
Author: User

Discuss the difference between copy and deepcopy in order to understand the reference in Python, the memory management of Python.

Everything in Python is an object, and the passing of a parameter is a reference to the object. This may sound more difficult to understand, compared to the assignment and reference in PHP has a general concept. Refer to the following paragraph for reference:

1. python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值——相当于通过“传引用”来传递对象。如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象——相当于通过“传值”来传递对象。 
2. 当人们复制列表或字典时,就复制了对象列表的引用同,如果改变引用的值,则修改了原始的参数。 
3. 为了简化内存管理,Python通过引用计数机制实现自动垃圾回收功能,Python中的每个对象都有一个引用计数,用来计数该对象在不同场所分别被引用了多少次。每当引用一次Python对象,相应的引用计数就增1,每当消毁一次Python对象,则相应的引用就减1,只有当引用计数为零时,才真正从内存中删除Python对象。

The so-called "pass value" is the meaning of the assignment. So what's special about Python parameter passing? See Example:

 >>> seq = [1, 2, 3]  
>>> seq_2 = seq  
>>> seq_2.append (4)  
>>> print seq, seq_2 [1,  2, 3, 4] [1, 2, 3, 4] 
>>> seq.append (5)  
>>> print seq, seq_2 [1, 2, 3, 4, 5] [1, 2, 3,  4, 5]

If you follow the syntax of PHP, the SEQ and seq_2 variables correspond to two different storage addresses, naturally corresponding to different values, is irrelevant, but in Python we are surprised. Look at the following example:

>>> a = 1 
>>> b = a 
>> > b = 2 
>>> print a, b 1 2 
>>>  c =  (1, 2)  
>>> d = c 
>>> d =   (1, 2, 3)  
>>> print c, d  (1, 2)   (1, 2,  3)

Is it obvious that there is a conflict with the above example? It is clear from the beginning of the reference that when the original object of the reference changes, they are not related, that is to say, they are two different object references, corresponding to the respective reference count plus minus 1, whereas in the first example SEQ and seq_2 are references to the original object [1, 2, 3] of the LIS object, So no matter append () or pop () does not change the original object, just change its elements, it is not difficult to understand the second example, because B = 2 is the creation of a new int object.

The following example looks at the difference between copy and deepcopy:

 >>> seq = [1, 2, 3]  
>>> seq_1 = seq  
>>> seq_2 = copy.copy (seq)  
>>> seq_3 =  Copy.deepcopy (seq)  
>>> seq.append (4)  
>>> print seq,  seq_1, seq_2, seq_3 [1, 2, 3, 4] [1, 2, 3, 4] [1,  2, 3] [1, 2, 3] 
>>> seq_2.append (5)  
>>>  Print seq, seq_1, seq_2, seq_3 [1, 2, 3, 4] [1, 2, 3,  4] [1, 2, 3, 5] [1, 2, 3] 
>>> seq_3.append (6)  
>>> print seq, seq_1, seq_2, seq_3 [1, 2, 3, 4 ] [1, 2, 3, 4] [1, 2, 3, 5] [1, 2, 3, 6]
This example does not see the link between copy and previous, nor does it show the difference between copy and deepcopy. So look again:
>>> m = [1, [' A '], 2]
>>> m_1 = m
>>> m_2 = copy.copy (M)
>>> m_3 = copy.deepcopy (M)
>>> m[1].append (' B ')
>>> print M, m_1, M_2, M_3 [1, [' A ', ' B '], 2] [1, [' A ', ' B '], 2] [1, [' A ', ' B '], 2] [1, [' A '], 2]
>>> m_2[1].append (' C ')
>>> print M, m_1, M_2, M_3 [1, [' A ', ' B ', ' C '], 2] [1, [' A ', ' B ', ' C '], 2] [1, [' A ', ' B ', ' C '], 2][1, [' A '], 2]
>>> m_3[1].append (' d ')
>>> print M, m_1, M_2, M_3 [1, [' A ', ' B ', ' C '], 2] [1, [' A ', ' B ', ' C '], 2] [1, [' A ', ' B ', ' C '], 2][1, [' A ', ' d '], 2]

from this we can see the difference, copy copy an object, but the object's properties or reference the original, deepcopy copy an object, the object inside the property is also copied, Deepcopy is completely another object. Let's look at an example:

>>> m = [1, [2, 2], [3, 3]]
>>> n = copy.copy (m)
>>> N[1].append (2)
>>> print m, n [1, [2, 2, 2], [3, 3]] [1, [2, 2, 2], [3, 3]]
>>> N[1] = 0
>>> print m, n [1, [2, 2, 2], [3, 3]] [1, 0, [3, 3]]
>>> N[2].append (3)
>>> print m, n [1, [2, 2, 2], [3, 3, 3] [1, 0, [3, 3, 3]]
>>> M[1].pop () 2
>>> print m, n [1, [2, 2], [3, 3, 3] [1, 0, [3, 3, 3]]
>>> M[2].pop () 3
>>> print m, n [1, [2, 2], [3, 3]] [1, 0, [3, 3]]

Finally test whether you are in control or not:

L = []
d = {' num ': 0, ' sqrt ': 0}
For x in [1, 2, 3]:
d[' num '] = X
d[' sqrt '] = x*x
L.append (d)
Print L

Since I am mainly engaged in web development, usually mainly with the framework of the package, rarely study python itself some things, but then, I am working with Python, is not specialized in the language of the professor and so on. The more time you find Python, the more interesting it is.

Reprint please specify the source, do not use for commercial purposes without permission.
This article connect address: http://luchanghong.com/python/2012/09/21/the-differences-between-copy-and-deepcopy-in-python.html

Python reference from the difference between copy and deepcopy in Python

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.