Shallow copy tạo object mới nhưng elements bên trong vẫn tham chiếu cùng objects gốc — thay đổi mutable elements sẽ ảnh hưởng cả hai bản.
- Deep copy tạo hoàn toàn độc lập, copy đệ quy toàn bộ.
- Dùng
copy.copy()cho shallow vàcopy.deepcopy()cho deep. - Lưu ý:
list.copy(),list[:],dict.copy()đều là shallow copy.
Shallow copy creates a new object but inner elements still reference the same objects — mutating nested elements affects both copies.
- Deep copy is fully independent, recursively copies everything.
- Use
copy.copy()for shallow,copy.deepcopy()for deep.
Pitfall: list.copy(), list[:], dict.copy() are all shallow copies.