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

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一书中关于深浅拷贝的错误的更多相关文章

  1. 关于《Head First Python》一书中print_lol()函数的思考

    关于<Head First Python>一书中print_lol()函数的思考 在<Head First Python>第一章中,讲述到Python处理复杂数据(以电影数据列 ...

  2. 天啦噜!仅仅5张图,彻底搞懂Python中的深浅拷贝

    Python中的深浅拷贝 在讲深浅拷贝之前,我们先重温一下 is 和==的区别. 在判断对象是否相等比较的时候我们可以用is 和 == is:比较两个对象的引用是否相同,即 它们的id 是否一样 == ...

  3. Python 中的深浅拷贝

    Python 中的深浅拷贝 参考文献:https://blog.csdn.net/lnotime/article/details/81194633 参考文献:https://blog.csdn.net ...

  4. js中的深浅拷贝

    js中的深浅拷贝 js中有深拷贝.浅拷贝一说,所谓的深浅拷贝是针对value类型为引用类型(函数.对象.数组)而言的,大概理解的就是: 浅拷贝: 拷贝出的对象c和原始对象o,c和o在key对应的val ...

  5. Python中的深浅拷贝

    1.什么是深浅拷贝? python中一切皆对象,python中的数字.字符串.元组等,如果存放在了内存中,这部分内存里面的内容是不会改变的,但是也有情况,内存中存放了可变对象,比如说列表和字典,他们的 ...

  6. Python基础【3】:Python中的深浅拷贝解析

    深浅拷贝 在研究Python的深浅拷贝区别前需要先弄清楚以下的一些基础概念: 变量--引用--对象(可变对象,不可变对象) 切片(序列化对象)--拷贝(深拷贝,浅拷贝) 我是铺垫~ 一.[变量--引用 ...

  7. 挖一挖python中的深浅拷贝问题

    前几天在做面试题的时候,遇到一个与Python深浅拷贝的问题,今天总结出来一个方法,能够快速判断在对一个对象复制后,新对象与原来对象是否会互相影响的方法. 先抛出结论,然后我们对结论进行验证~~~ 先 ...

  8. 浅谈Python中的深浅拷贝的区别

    深.浅拷贝总结 深拷贝 拷贝可变数据类型,如列表容器: a = [1, 2, [3, 4]] b = copy.deepcopy(a) a 与 b 所指的列表容器的空间地址不一致,即 id(a) != ...

  9. python基础知识5——赋值与深浅拷贝——整数和字符串,列表元组字典

    深浅copy 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 import copy 2 # ######### 数字.字符串 #### ...

随机推荐

  1. js变量的生命周期

    1.在JavaScript中,对于for循环中定义的i变量,其生命周期在循环结束后仍然是有效的. for (var i=0; i < 10; i++){ doSomething(i); } al ...

  2. angularjs bind与model配合双向绑定 表达式方法输出

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  3. [LeetCode] Toeplitz Matrix 托普利兹矩阵

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  4. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  5. Linux使用踩坑记

    Ubuntu安装坑: 1.对于新手第一次安装ubuntu,特殊情况会出现因为分辨率问题导致安装界面不全,无法进行下一步操作. 解决方案:使用alt+鼠标左键拖动屏幕Linux文件名乱码问题: 2.因为 ...

  6. TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    我使用VS2013  Python3.5  TensorFlow 1.3  的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...

  7. [POJ 3487]The Stable Marriage Problem

    Description The stable marriage problem consists of matching members of two different sets according ...

  8. [BJOI2006]狼抓兔子

    题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  9. 注意类型转换——Effective C++

    注意类型转换: C++提供了四种新式类型转换: const_cast<T>(expression); static_cast<T>(expression); dynamic_c ...

  10. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...