a = [1,2,3,4]
print(id(a))
b = a
print(id(b))
# 地址相同
a.append('a')
print(a)
print(b)#b和a的值一致,a改变,b就跟着改变
'''
2342960103104
2342960103104
[1, 2, 3, 4, 'a']
[1, 2, 3, 4, 'a']
'''
浅拷贝对不可变类型和可变类型的copy不同
import copy
a = [1,2,3]
b = copy.copy(a)
a.append('a')
print(a," ",b)
print(id(a),id(b))
a = (1,2,3)
b = copy.copy(a)
print(id(a),id(b))
# 浅拷贝copy.copy()对于可变类型赋予的地址不同,对于不可变类型赋予相同地址
'''
[1, 2, 3, 'a'] [1, 2, 3]
2053176165376 2053176165568
2053175778688 2053175778688
'''
深拷贝
import copy
a = [1,2,3,4]
print(id(a))
b = copy.deepcopy(a)
print(id(b))#地址不同
a.append('a')
print(a," ",b)
# 深拷贝:不跟着拷贝的对象发生变化
'''
2944424869376
2944424869568
[1, 2, 3, 4, 'a'] [1, 2, 3, 4]
'''
2020-05-08