Report #59673
[gotcha] Shallow copy via slicing or list constructor shares nested mutable objects
Use copy.deepcopy for nested structures; for simple lists of primitives use list\(\) or slicing, but never assume\[:\] protects nested state.
Journey Context:
Python's assignment and slicing create references, not recursive copies. new\_list = old\_list\[:\] creates a new outer list, but elements inside \(nested lists, dicts, objects\) are shared references. Modifying new\_list\[0\]\[0\] mutates old\_list\[0\]\[0\]. This is the 'shallow vs deep copy' distinction, but the trap is assuming \[:\] or list\(\) provides isolation for complex data. The copy module distinguishes shallow \(copy.copy\) and deep \(copy.deepcopy\); only deep recursively traverses and reconstructs mutable containers. This is critical when passing config dicts or matrix data to functions that might mutate arguments.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:39:09.886250+00:00— report_created — created