Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述:
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
>>>
可见,深拷贝对于字符串的处理都是返回相同对象,更不用说浅拷贝那三种形式了.
Core Python Programming一书中关于深浅拷贝的错误的更多相关文章
- 关于《Head First Python》一书中print_lol()函数的思考
关于<Head First Python>一书中print_lol()函数的思考 在<Head First Python>第一章中,讲述到Python处理复杂数据(以电影数据列 ...
- 天啦噜!仅仅5张图,彻底搞懂Python中的深浅拷贝
Python中的深浅拷贝 在讲深浅拷贝之前,我们先重温一下 is 和==的区别. 在判断对象是否相等比较的时候我们可以用is 和 == is:比较两个对象的引用是否相同,即 它们的id 是否一样 == ...
- Python 中的深浅拷贝
Python 中的深浅拷贝 参考文献:https://blog.csdn.net/lnotime/article/details/81194633 参考文献:https://blog.csdn.net ...
- js中的深浅拷贝
js中的深浅拷贝 js中有深拷贝.浅拷贝一说,所谓的深浅拷贝是针对value类型为引用类型(函数.对象.数组)而言的,大概理解的就是: 浅拷贝: 拷贝出的对象c和原始对象o,c和o在key对应的val ...
- Python中的深浅拷贝
1.什么是深浅拷贝? python中一切皆对象,python中的数字.字符串.元组等,如果存放在了内存中,这部分内存里面的内容是不会改变的,但是也有情况,内存中存放了可变对象,比如说列表和字典,他们的 ...
- Python基础【3】:Python中的深浅拷贝解析
深浅拷贝 在研究Python的深浅拷贝区别前需要先弄清楚以下的一些基础概念: 变量--引用--对象(可变对象,不可变对象) 切片(序列化对象)--拷贝(深拷贝,浅拷贝) 我是铺垫~ 一.[变量--引用 ...
- 挖一挖python中的深浅拷贝问题
前几天在做面试题的时候,遇到一个与Python深浅拷贝的问题,今天总结出来一个方法,能够快速判断在对一个对象复制后,新对象与原来对象是否会互相影响的方法. 先抛出结论,然后我们对结论进行验证~~~ 先 ...
- 浅谈Python中的深浅拷贝的区别
深.浅拷贝总结 深拷贝 拷贝可变数据类型,如列表容器: a = [1, 2, [3, 4]] b = copy.deepcopy(a) a 与 b 所指的列表容器的空间地址不一致,即 id(a) != ...
- python基础知识5——赋值与深浅拷贝——整数和字符串,列表元组字典
深浅copy 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 import copy 2 # ######### 数字.字符串 #### ...
随机推荐
- .NET Core2.0+MVC 用Redis/Memory+cookie实现的sso单点登录
之前发布过使用session+cookie实现的单点登录,博主个人用的很不舒服,为什么呢,博主自己测试的时候,通过修改host的方法,在本机发布了三个站点,但是,经过测试,发现,三个站点使用的sess ...
- 分布式版本管理工具 git常用命令
Git global setup git config --global user.name "joey" git config --global user.email " ...
- Lintcode388 Permutation Sequence solution 题解
[题目描述] Given n and k, return the k-th permutation sequence. Notice:n will be between 1 and 9 inclusi ...
- [POI2006]OKR-Periods of Words
题目描述 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, 使得 A = PB. 如果 P A 并且 P 不是一个空串,那么我们说 P 是A ...
- 51 nod 1515 明辨是非(并查集合并)
1515 明辨是非题目来源: 原创基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以 ...
- ●BZOJ 1692 [Usaco2007 Dec]队列变换
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1692 题解: 后缀数组,贪心由于每次可以取出旧队列的首部或尾部放在新队列的尾部.所以就需要比 ...
- bzoj1858[Scoi2010]序列操作 线段树
1858: [Scoi2010]序列操作 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 3079 Solved: 1475[Submit][Statu ...
- Android 自定义支持快速搜索筛选的选择控件(一)
Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...
- Architecture : Describable Command and Identifiable Data
Architecture : Describable Command and Identifiable Data Description Terms Command A command is a fu ...
- ubuntu + 不识别无线网卡简易处理方式 + 需有线联网
进入ubuntu, 连接有线网; 系统设置 -> 软件和更新 -> 附加驱动 -> 找到无线网卡驱动并更新 若无候选的无线网卡驱动,我就没招了.