python deep copy and shallow copy
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings between a target and an object.)。对于可变对象来说,当一个改变的时候,另外一个不用改变,因为是同时的,也就是说是保持同步的。
此时不想让他们同步的话可以使用copy模块,copy.copy()浅拷贝,copy.deepcopy()深拷贝。
前者是对整个对象的拷贝,对子对象仍然是引用,后者是对对象以及子对象的拷贝。具体一些可以下面例子:
In [26]: import copy [6/165] In [27]: a = [1, 2, [3, 4]] In [28]: b = a # 引用 In [29]: c = copy.copy(a) # shallow copy, 对1,2,【3,4】拷贝,对【3,4】中的元素是引用 In [30]: d = copy.deepcopy(a) # deep copy 对1,2,【3,4】都是拷贝,【3,4】中的元素也是拷贝 In [31]: b
Out[31]: [1, 2, [3, 4]] In [32]: c
Out[32]: [1, 2, [3, 4]] In [33]: d
Out[33]: [1, 2, [3, 4]] In [34]: a.append(5) In [35]: a
Out[35]: [1, 2, [3, 4], 5] In [36]: b
Out[36]: [1, 2, [3, 4], 5] In [37]: c
Out[37]: [1, 2, [3, 4]] In [38]: d
Out[38]: [1, 2, [3, 4]] In [39]: a[2].append(6) In [40]: a
Out[40]: [1, 2, [3, 4, 6], 5] In [41]: b
Out[41]: [1, 2, [3, 4, 6], 5] In [42]: c
Out[42]: [1, 2, [3, 4, 6]] In [43]: d
Out[43]: [1, 2, [3, 4]]
一些对象的浅拷贝可以通过其他方式实现,如字典可以使用 dict.copy(),列表使用listb = lista[:],如下:
In [46]: a = {'name': 'wang',} In [47]: b = dict.copy(a) In [48]: b
Out[48]: {'name': 'wang'} In [49]: a.update({'age': 13}) In [50]: a
Out[50]: {'age': 13, 'name': 'wang'} In [51]: b
Out[51]: {'name': 'wang'} In [52]: a = {'name': {'first_name': 'wang'}} In [53]: b = dict.copy(a) In [54]: b
Out[54]: {'name': {'first_name': 'wang'}} In [55]: a['name'].update({'last_name': 'Emma'}) In [56]: a
Out[56]: {'name': {'first_name': 'wang', 'last_name': 'Emma'}} In [57]: b
Out[57]: {'name': {'first_name': 'wang', 'last_name': 'Emma'}} In [58]: lista = [1, 2] In [59]: listb = lista[:] In [60]: lista.append(3) In [61]: lista
Out[61]: [1, 2, 3] In [62]: listb
Out[62]: [1, 2] In [63]: lista = [1, 2, [3, 4]] In [64]: listb = lista[:] In [65]: lista[2].append(5) In [66]: listb
Out[66]: [1, 2, [3, 4, 5]]
注意:
深拷贝浅拷贝都不能拷贝文件,模块等类型(This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged;)
同时可以自定义这两个函数~
参考:
https://docs.python.org/2/library/copy.html
http://www.cnblogs.com/coderzh/archive/2008/05/17/1201506.html
http://blog.csdn.net/sharkw/article/details/1934090
python deep copy and shallow copy的更多相关文章
- [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝
13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...
- Summary: Deep Copy vs. Shallow Copy vs. Lazy Copy
Object copy An object copy is an action in computing where a data object has its attributes copied t ...
- deep copy and shallow copy
链接A:浅拷贝就是成员数据之间的一一赋值:把值赋给一一赋给要拷贝的值.但是可能会有这样的情况:对象还包含资源,这里的资源可以值堆资源,或者一个文件..当值拷贝的时候,两个对象就有用共同的资源,同时对资 ...
- python中的shallow copy 与 deep copy
今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...
- 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考
首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME copy - Generic (shallow and dee ...
- Shallow copy and Deep copy
Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...
- shallow copy 和 deep copy 的示例
本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481874.html (Robin) Student package base; impo ...
- shallow copy & deep copy
1.深复制与浅复制的概念 ->浅复制(shallow copy)概念 在SDK Guides中(搜索copy),官方给出的浅复制概念为: Copying compound objects, ...
- JavaScript 深拷贝(deep copy)和浅拷贝(shallow copy)
参考: [进阶4-1期]详细解析赋值.浅拷贝和深拷贝的区别 How to differentiate between deep and shallow copies in JavaScript 在编程 ...
随机推荐
- Struts2 动态结果和带参数的跳转
完整代码:Struts16ActionResultsDemo.rar 1.动态结果. 有时我们需要在Action里取得我个要转跳的页面 看一下我们的struts.xml <?xml versio ...
- <实训|第八天>超级管理员管理linux用户行为权限附监控主机状态
作为运维工程师,系统管理员,你最大的权力就是给别人分配权力,而且你还能时时控制着他们,今天就给大家介绍一下关于管理用户这一方面的前前后后. 开班第八天: 主要课程大纲:(下面我将把自己的身份定位成一 ...
- 学习SQLite之路(三)
20160616更新 参考: http://www.runoob.com/sqlite/sqlite-tutorial.html 1. SQLite PRAGMA:可以用在 SQLite 环境内控制 ...
- 理解JavaScript的作用域链
上一篇文章中介绍了Execution Context中的三个重要部分:VO/AO,scope chain和this,并详细的介绍了VO/AO在JavaScript代码执行中的表现. 本文就看看Exec ...
- nios II--实验7——数码管IP软件部分
软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...
- golang: 把sql结果集以json格式输出
func getJSON(sqlString string) (string, error) { stmt, err := db.Prepare(sqlString) if err != nil { ...
- extjs 箱子布局
a.flex 配置项 flex 配置项不是设置在布局上,而是设置在子项的配置项.每个子项相对的 flex 值都会与全体子项 flex 累加的值相比较,根据此结果,处理每个子项的 flex 最后是多少. ...
- jQuery基础--样式篇(1)
1.jQuery简介:JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...
- Java反射机制学习与研究
Java反射机制:可以获取正在运行时的Java对象. 1.判断运行时对象对象所属的类. 2.判断运行时对象所具有的成员变量和方法. 3.还可以调用到private方法,改变private变量的值. S ...
- Linux基础知识集锦
查看当前进程ID与当前进程的父进程ID $$ echo $PPID shell脚本之for循环 for ((i=0;i<10;++i)) do echo "hello",$i ...