import copy
alist = [1,2,3,[“a”,“b”]]
b = alist
print(b) #[1, 2, 3, [‘a’, ‘b’]]
alist.append(5)
print(alist,b) #[1, 2, 3, [‘a’, ‘b’], 5] [1, 2, 3, [‘a’, ‘b’], 5]
#b和list 引用的相同的list 所以会相互改变 [1, 2, 3, [‘a’, ‘b’], 5, 6] [1, 2, 3, [‘a’, ‘b’], 5, 6]
b.append(6)
print(alist,b)
c = copy.copy(alist)
print(alist,c)
alist.append(5) #非可变对象 不会改变c
print(alist,c)
alist[3].append(‘cccc’) #子对象的list为公用可变 所以c会跟着改变
print(alist,c)
#alist 和 d 完全独立于所有对象
d = copy.deepcopy(alist)
print(alist,d) #[1, 2, 3, [‘a’, ‘b’]] [1, 2, 3, [‘a’, ‘b’]]
alist.append(5)
print(alist,d) #[1, 2, 3, [‘a’, ‘b’], 5] [1, 2, 3, [‘a’, ‘b’]]
alist[3].append(‘cccc’)
print(alist,d) #[1, 2, 3, [‘a’, ‘b’, ‘cccc’], 5] [1, 2, 3, [‘a’, ‘b’]]