python核心编程----读书笔记

1,is , == 的区别

a is b:指的是a和b指向内存的同一空间;a == b:指的是a和b所指向的值是相等的。直接上代码

>>> a = [1,2,3]
>>> b = [1,2,3]
>>> c = a
>>> a == b
True
>>> a == c
True
>>> a is b
False
>>> a is c
True

2,copy与deepcopy的区别

a = b是浅拷贝,浅拷贝不会另外的将内容拷贝一份,只是类似于指向复制,深拷贝是完完全复制出来。

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [a,b]
>>> import copy
>>> d = copy.copy(c)
>>> id(c)
5697224
>>> id(d)
47890312
>>> id(c[0])
47968456
>>> id(d[0])
47968456
>>> e = copy.deepcopy(c)
>>> id(e)
45702920
>>> id(e[0])
45715592

python核心编程----读书笔记_第1张图片

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/yl_mouse

你可能感兴趣的:(python核心编程)