深拷贝,浅拷贝
- python中赋值是按引用传递的
- 深拷贝 copy.deepcopy(),拷贝对象和子对象
- 浅拷贝 copy.copy(),拷贝对象但是不拷贝子对象
import copy
a = [1, 2, 3, ['a', 'b']]
b = a
c = copy.copy(a)
d = copy.deepcopy(a)
a.append(5)
a[3].append('c')
print('a= ', a)
print('b= ', b)
print('c= ', c)
print('d= ', d)
输出:
('a= ', [1, 2, 3, ['a', 'b', 'c'], 5])
('b= ', [1, 2, 3, ['a', 'b', 'c'], 5])
('c= ', [1, 2, 3, ['a', 'b', 'c']])
('d= ', [1, 2, 3, ['a', 'b']])