python基础Python 运维生活不易 我用python

图解 Python 深拷贝和浅拷贝

2017-05-08  本文已影响58人  码农小杨

原文出处:http://www.cnblogs.com/wilber2013/p/4645353.html

Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果。

下面我们按照原文,细细理解下这些差别:

对象赋值

我们看下面的的代码部分:

In [1]: will = ["will",28,["python","C#","Javascript"]]

In [2]: wilber = will

In [3]: id(will)
Out[3]: 2335727905096

In [4]: id(wilber)
Out[4]: 2335727905096

In [5]: print([id(ele) for ele in will])
[2335725285536, 1453458736, 2335727904456]

In [6]: print([id(ele) for ele in wilber])
[2335725285536, 1453458736, 2335727904456]

In [7]: will[0] = "wilber"

In [8]: will[2].append("CSS")

In [9]: id(will)
Out[9]: 2335727905096

In [10]: id(wilber)
Out[10]: 2335727905096

In [11]: print([id(ele) for ele in will])
[2335727892328, 1453458736, 2335727904456]

In [12]: print([id(ele) for ele in wilber])
[2335727892328, 1453458736, 2335727904456]

我们分析下这段代码:

可以理解为,Python中,对象的赋值都是进行对象引用(内存地址)传递

这里需要注意的一点是,str是不可变类型,所以当修改的时候会替换旧的对象,产生一个新的地址。

为了便于理解,我将原文的图片直接拷贝过来,里面内存地址编号和代码不一致。

Paste_Image.png
浅拷贝

下面看看浅拷贝

In [1]: import copy

In [2]: will = ["Will", 28, ["Python", "C#", "JavaScript"]]

In [3]: wilber = copy.copy(will)

In [4]: id(will)
Out[4]: 2899569681288

In [5]: id(wilber)
Out[5]: 2899583552712

In [6]: print([id(ele) for ele in will])
[2899583263664, 1453458736, 2899585719944]

In [7]: print([id(ele) for ele in wilber])
[2899583263664, 1453458736, 2899585719944]

In [8]: will[0] = "wilber"

In [9]: will[2].append("CSS")

In [10]: id(will)
Out[10]: 2899569681288

In [11]: id(wilber)
Out[11]: 2899583552712

In [12]: print([id(ele) for ele in will])
[2899586038616, 1453458736, 2899585719944]

In [13]: print([id(ele) for ele in wilber])
[2899583263664, 1453458736, 2899585719944]

In [14]: will
Out[14]: ['wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]

In [15]: wilber
Out[15]: ['Will', 28, ['Python', 'C#', 'JavaScript', 'CSS']]

分析下这段代码:

浅拷贝会创建一个新的对象,这个例子中”wilber is not will”
但是,对于对象中的元素,浅拷贝就只会使用原始元素的引用(内存地址),也就是说”wilber[i] is will[i]”

Paste_Image.png

总结一下,当我们使用下面的操作的时候,会产生浅拷贝的效果:

深拷贝

最后我们看看深拷贝

In [1]: import copy

In [2]:  will = ["Will", 28, ["Python", "C#", "JavaScript"]]

In [3]: wilber = copy.deepcopy(will)

In [4]: id(will)
Out[4]: 2871945438664

In [5]: id(wilber)
Out[5]: 2871945199048

In [6]: print([id(ele) for ele in will])
[2871945176264, 1453458736, 2871945207496]

In [7]: print([id(ele) for ele in wilber])
[2871945176264, 1453458736, 2871945341256]

In [8]: will[0] = "wilber"

In [9]: will[2].append("CSS")

In [10]: will
Out[10]: ['wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]

In [11]: id(will)
Out[11]: 2871945438664

In [12]: id(wilber)
Out[12]: 2871945199048

In [13]: wilber
Out[13]: ['Will', 28, ['Python', 'C#', 'JavaScript']]

In [14]: print([id(ele) for ele in will])
[2871945496928, 1453458736, 2871945207496]

In [15]: print([id(ele) for ele in wilber])
[2871945176264, 1453458736, 2871945341256]

分析一下这段代码:

跟浅拷贝类似,深拷贝也会创建一个新的对象,这个例子中”wilber is not will”
但是,对于对象中的元素,深拷贝都会重新生成一份(有特殊情况,下面会说明),而不是简单的使用原始元素的引用(内存地址)

例子中will的第三个元素指向2871945207496,而wilber的第三个元素是一个全新的对象2871945341256,也就是说,”wilber[2] is not will[2]”

但是list的第三个元素是一个可变类型,修改操作不会产生新的对象,但是由于”wilber[2] is not will[2]”,所以will的修改不会影响wilber

Paste_Image.png
拷贝的特殊情况

其实,对于拷贝有一些特殊情况:

In [16]:  book =("python","c#","Javascript")

In [17]: copies = copy.deepcopy(book)

In [18]: book is copies
Out[18]: True

In [19]:  book =("python","c#","Javascript",[])

In [20]: copies = copy.deepcopy(book)

In [21]: book is copies
Out[21]: False

本文介绍了对象的赋值和拷贝,以及它们之间的差异:

上一篇下一篇

猜你喜欢

热点阅读