| Data | Copy Behavior |
|---|---|
| Immutable |
|
| Mutable |
|
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Scope | Only copies the outer container, does not copy nested elements. | Recursively copies the outer container and all nested elements, including multi-level nested mutable types. |
| Memory | Nested mutable elements share the same memory address. | No memory sharing at all, the new object is completely independent from the original. |
| Impact | Modifying nested mutable elements will affect the original object. | Modifying either the new or the original object will not affect the other. |
| Implementation | copy.copy(), [:], or foo.copy() if supported |
copy.deepcopy(), the only built-in valid method. |
| Performance | Lower overhead. | Higher overhead due to recursive copying. |
copy.copy() and copy.deepcopy() directly
__copy__(self) and __deepcopy__(self, memo) for customized behavior