Computer Science

Confusing Shallow Copy and Deep Copy

A student writes b = a.copy() and expects changes inside b's nested objects to leave a unchanged.

Quick check

Why students make it

The misleading intuition is that copying a container also copies everything it points to. In many languages, a copy operation only duplicates one level unless a deep-copy operation is requested.

The rule done right

A shallow copy creates a new outer container but keeps references to the same nested objects. A deep copy recursively copies nested objects so independent mutation of nested state does not affect the original.