python 浅copy与深copy_python 进阶篇 浅拷贝与深拷贝

阐述引用、浅拷贝和深拷贝前,首先需要要了解 Python 的世界里,一切皆对象,每个对象各包含一个 idendity、type 和 value。

7468972503e8f2d860390decb41b79aa.png

引用(Reference)

a5ae2f6b8d6331beebaded89979c0da3.png

>>> b = [1 , 2]

>>> a = [b, 3, 4]

>>>

>>> c = a

>>> print c

[[1, 2], 3, 4]

>>> id(a)

4408148552

>>> id(c)

4408148552

c = a 表示 c 和 a 指向相同的地址空间,并没有创建新的对象。

浅拷贝(Shallow copy)

常见的浅拷贝的方法,是使用数据类型本身的构造器(list, tuple, dict, set),对于可变的序列(list, tuple),我们还可以通过切片操作符':'完成浅拷贝

当然,Python 中也提供了相对应的函数 copy.copy(),适用于任何数据类型。

dd21efd45837e2f3a227f13f92124ddb.png

>>> import copy

>>> d = copy.copy(a)

>>> print d

[[1, 2], 3, 4]

>>>

>>> id(a)

4408148552

>>> id(d)

4408199792

>>>

>>> id(a[0])

4408022944

>>> id(d[0])

4408022944

>>>

>>> d[0][0] = 5

>>> print a

>>> [[5, 2], 3, 4]

d = copy.copy(a) 创建了一个新对象,复制了原有对象的引用。

深拷贝(Deep copy)

是指重新分配一块内存,创建一个新的对象,并且将原对象中的元素,以递归的方式,通过创建新的子对象拷贝到新对象中。因此,新对象和原对象没有任何关联。

96f3b95982bb7549f6a33206326d9bc2.png

>>> e = copy.deepcopy(a)

>>> print e

>>> [[1, 2], 3, 4]

>>>

>>> id(a)

>>> 4408148552

>>> id(e)

>>> 4408394792

>>>

>>> id(a[0])

>>> 4408022944

>>> id(e[0])

>>> 4408398432

>>>

>>> e[0][0] = 5

>>> print a

>>> [[1, 2], 3, 4]

e = copy.deepcopy(a) 新建了一个新对象,完整的在内存中复制原有对象。

Note

关于浅拷贝和深拷贝的区别,Python 的 document 是这样解释的:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

使用深拷贝时,需要注意以下两个问题:

递归对象拷贝: Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.

大对象拷贝: Because deep copy copies everything it may copy too much, e.g., administrative data structures that should be shared even between copies.

思考题

深度拷贝一个无限嵌套的列表。那么。当我们用等于操作符'=='进行比较时,输出会是什么呢?是 True 或者 False 还是其他?为什么呢?

import copy

x = [1]

x.append(x)

y = copy.deepcopy(x)

# 以下命令的输出是?

x == y

程序会报错:RecursionError: maximum recursion depth exceeded in comparison。因为x是一个无限嵌套的列表,y深度拷贝x也是一个无限嵌套的列表,理论上x==y应该返回True,但是x==y内部执行是会递归遍历列表x和y中每一个元素的值,由于x和y是无限嵌套的,因此会stack overflow,报错

参考

大部分内容参考了博客,在基础上做了补充。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值