python deep copy and shallow copy
2016-06-21 15:40
711 查看
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings between a target and an object.)。对于可变对象来说,当一个改变的时候,另外一个不用改变,因为是同时的,也就是说是保持同步的。
此时不想让他们同步的话可以使用copy模块,copy.copy()浅拷贝,copy.deepcopy()深拷贝。
前者是对整个对象的拷贝,对子对象仍然是引用,后者是对对象以及子对象的拷贝。具体一些可以下面例子:
一些对象的浅拷贝可以通过其他方式实现,如字典可以使用 dict.copy(),列表使用listb = lista[:],如下:
注意:
深拷贝浅拷贝都不能拷贝文件,模块等类型(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
此时不想让他们同步的话可以使用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编程中namedtuple类的用法
- 阿里云Ubuntu系统安装python3.5过程
- Python编程中实现迭代器的一些技巧小结
- python学习笔记之函数总结--高阶函数以及装饰器
- Python 小练习_battleship
- python mysqldb 模块学习
- python logging 转载
- python之map映射
- Python的Django框架中forms表单类的使用方法详解
- 数字的正则表达式,python
- Python爬虫实践(八):正则表达式re模块(2)
- python练习实例之二 利润分红计算
- Python中将函数作为另一个函数的参数传入并调用
- Python 连接Sql Server数据库 MSSql
- Python 连接Sql Server数据库 MSSql
- python远程连接mysql服务器
- 朴素贝叶斯分类器
- python多线程和多进程对比
- numpy.array 操作简单总结
- MNIST 可视化