Core Python Programming一书中关于深浅拷贝的错误

本文通过实例详细分析了Python中浅拷贝与深拷贝的区别,并针对字符串拷贝进行了深入探讨,指出了常见误解。

 

该书关于深浅拷贝的论述:

6.20. *Copying Python Objects and Shallow and Deep Copies

"when shallow copies are made, the string is explicitly copied and a new (string) object created"

这是错的.当一个容器对象被浅拷贝,字符串也是和列表一样,都只是建立引用.

奇特的是,作者在这句话之前写有自相矛盾的句子:

A shallow copy of an object is defined to be a newly created object of the same 
type as the original object whose contents are references to the elements in the 
original object.

当然,和错误的东西相矛盾,那么自然是正确的了.

直接上例子:

>>> r=['a',[1]]
>>> s=r[:]
>>> r==s
True
>>> r is s
False
>>> id(r)
35316200
>>> id(s)
35311128

>>> id(r[0]),id(s[0]),   
(19811840, 19811840)    #id相同,说明r[0]虽然是字符串对象,但是在浅拷贝过程中仍然是增加引用,并非在s[0]位置上建立了一个新的字符串对象
>>> id(r[1]),id(s[1]),
(35316120, 35316120)

>>> r[0]='cg'
>>> r,s
(['cg', [1]], ['a', [1]])

>>> r[1].append(2)
>>> r,s
(['cg', [1, 2]], ['a', [1, 2]])

>>> r[1]=[]
>>> r,s
(['cg', []], ['a', [1, 2]])
>>> 

 更多说明:

>>> a='a'
>>> b='a'
>>> a is b
True
>>> a='啊啊啊士大夫'
>>> b='啊啊啊士大夫'
>>> a is b
False
>>> a='absfdsdfsdfs'
>>> b='absfdsdfsdfs'
>>> a is b
True
>>> a='啊啊啊士大夫'
>>> b=str(a)
>>> a is b
True
>>> c=a[:]
>>> a is c
True
>>> import copy
>>> d=copy.copy(a)
>>> a is d
True
>>> e=copy.deepcopy(a)
>>> a is e
True
>>> f=(a + ' ')[0:-1]
>>> a is f
False
>>> a==f
True
>>> 

SO上解释得很好:

If you make a copy of a string itself, then the string still need not actually be copied (since it's immutable). As an optimization you could just be given back the same object again, and for most purposes that's just as good. Indeed, Python 2.7.5 and Python 3.2.5 (which are what I have installed here) both make that optimization on all three of original[:]str(original) and copy.copy(original).

If for some obscure reason you actually want two equal strings that are not identical (perhaps to test the performance of == or something, I don't know), then you basically have to try to trick the Python runtime: (original + ' ')[0:-1] or whatever.

我们来看看是什么意思:

>>> a='测试字符'
>>> b=copy.deepcopy(a)
>>> a is b
True
>>> a=[1,2]
>>> b=copy.deepcopy(a)
>>> a is b
False
>>> 

可见,深拷贝对于字符串的处理都是返回相同对象,更不用说浅拷贝那三种形式了.

转载于:https://www.cnblogs.com/xiangnan/p/3392587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值