深浅copy

浅拷贝 只copy了一层 可哈希的变量改变则二者不同   不可哈希变量改变二者都改变   (第一层数据不水质变化而变化(共用一个ID)    其它层随其变化而变化(ID 不同))
深拷贝  全部都copy了 不论什么值改变 原值都改变呢   (无论第几层都不变(共用一个ID))

变量赋值、浅拷贝、深拷贝

1,创建变量

1.1不可变对象

a = 'hello'

b = 'hello'

print id(a)

print id(b)

执行结果:

>>>

43461664

43461664

>>> 

结果:a和b的id一样。因为创建b的时候解释器发现b的值是hello,hello是字符串,字符串是不可变对象。hello这个值在内存中已经有了,就不重新开辟内存空间了,所以就把b的引用指向了a的内存空间

1.2可变对象

a = ['hello']

b = ['hello']

print id(a)

print id(b)

内存图:

执行结果:

>>>

43140880

39227920

>>> 

结论:a和b的id不一样。因为创建b的时候解释器发现b的值是[hello],[hello]是list,list是可变对象。所以直接开了一个新的内存空间

2,重新赋值

2.1不可变对象的赋值

a = 'hello'

print id(a)

a = 'world'

print id(a)

执行结果:

>>>

41495584

41495648

>>> 

内存图:

2.2可变对象的赋值

a = ['hello']

print id(a)

a[0] = 'world'

print id(a)

执行结果:

>>>

42616592

42616592

>>> 

内存图:

3,变量对变量的赋值

3.1不可变变量的赋值

a = 'hello'

b = 'hello'

a = b

print id(a)

print id(b)

执行结果:

>>>

41298976

41298976

>>> 

3.2可变变量的赋值

a = ['hello']

print id(a)

b = ['hello']

#下面这一行会把b的引用赋值给a,然后把a的内存空间释放,a和b一起用b的内存空间

a = b             

print id(a)

print id(b)

执行结果:

>>>

42157840

42157480

42157480

>>>

4,浅拷贝

浅拷贝会把子元素里面的可变元素弄成相同地址

list1 = ['hello',['world']]

list2 = list1[:]

print id(list1),list1

print id(list2),list2

print '-'*30

print id(list1[0]),u'id of list1-hello'

print id(list2[0]),u'id of list2-hello'

print '-'*30

print id(list1[1]),u'id of list1-world'

print id(list2[1]),u'id of list2-world'

执行结果:

40145424 ['hello', ['world']]

44057104 ['hello', ['world']]

------------------------------

43985952 id of list1-hello

43985952 id of list2-hello

------------------------------

44058384 id of list1-world

44058384 id of list2-world

结论:list1和list2地址不同,但是两个list里面的hello和world都相同,说明List是两份,但是里面的内容是1份

5,深拷贝

深拷贝会把子元素里面的可变元素弄成不同地址

5.1深拷贝

from copy import deepcopy

list1 = ['hello',['world']]

list2 = deepcopy(list1)

print id(list1),list1

print id(list2),list2

print '-'*30

print id(list1[0]),u'id of list1-hello'

print id(list2[0]),u'id of list2-hello'

print '-'*30

print id(list1[1]),u'id of list1-world'

print id(list2[1]),u'id of list2-world'

执行结果:

>>>

38900240 ['hello', ['world']]

42811920 ['hello', ['world']]

------------------------------

42740768 id of list1-hello

42740768 id of list2-hello

------------------------------

42813200 id of list1-world

42813760 id of list2-world

>>> 

结论:深拷贝会把所有可变对象从新开辟内存空间,包含多层嵌套的可变对象,但是不可变对象还是只有1个地址

5.2深拷贝多加一层嵌套的例子

list1 = ['hello',['world',['abc']]]

list2 = deepcopy(list1)

print id(list1),list1

print id(list2),list2

print '-'*30

print id(list1[0]),u'id of list1-hello'

print id(list2[0]),u'id of list2-hello'

print '-'*30

print id(list1[1][1]),u'id of list1-abc'

print id(list2[1][1]),u'id of list2-abc'

执行结果:

>>>

41894416 ['hello', ['world', ['abc']]]

41908624 ['hello', ['world', ['abc']]]

------------------------------

41823264 id of list1-hello

41823264 id of list2-hello

------------------------------

41895696 id of list1-abc

41896536 id of list2-abc

>>> 

原文地址:https://www.cnblogs.com/qj696/p/10617564.html