Shallow vs. Deep Copy
CONCEPTS:Python Copy Semantics
Question Variations
- "What is the difference between `=` and `copy.copy()`?"
- "Why does copying a nested list sometimes still share changes?"
- "When should you avoid `deepcopy`?"
- "How can a class customize its copying behavior?"
Why This Is Asked
Copying bugs commonly appear when nested configuration, request data, or object graphs are mutated unexpectedly. This question checks whether you can trace object references and choose copying deliberately instead of assuming an assignment creates a new value.
Key Concepts
- Assignment creates another reference to the same object.
- A shallow copy creates a new outer object but shares nested references.
copy.deepcopyrecursively copies nested objects and tracks cycles.- Immutable values often do not need copying; copying can be costly for large graphs.
Question Variations
- “What is the difference between
=andcopy.copy()?” - “Why does copying a nested list sometimes still share changes?”
- “When should you avoid
deepcopy?” - “How can a class customize its copying behavior?”