References and copying
Predict which changes are shared and choose shallow or deep copies intentionally.
On this page
The concept
Assignment does not copy a collection. It binds another name to the same object. Mutation through either name is visible through both. A shallow copy creates a new outer collection but retains references to its nested members. A deep copy recursively copies supported nested data.
Use == to compare values and is to compare identity. Checking value is None is the standard missing-value test. Deepcopy is useful, but it is not a substitute for clear ownership: designing functions to produce new results can make fewer copies and clearer contracts possible.
Worked example
from copy import deepcopy
original = [1, 2]
alias = original
alias.append(3)
print(original, alias is original)
separate = original.copy()
separate.append(4)
print(original, separate)
nested = [[1], [2]]
shallow = nested.copy()
deep = deepcopy(nested)
shallow[0].append(9)
print("Original:", nested, "Deep copy:", deep)
missing = None
print(missing is None)Trace the result
- alias and original are the same list, so appending through alias changes the original.
- separate is a new outer list and its append is independent.
- shallow shares nested[0], while deep keeps an independent nested list.
[1, 2, 3] True
[1, 2, 3] [1, 2, 3, 4]
Original: [[1, 9], [2]] Deep copy: [[1], [2]]
TrueLine-by-line walkthrough
Line 1: from copy import deepcopy
Import deepcopy from copy. This supplies independent copies of supported nested data. The caller can now use the imported names directly.
Line 3: original = [1, 2]
Evaluate [1, 2], then bind or update original with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 4: alias = original
Evaluate original, then bind or update alias with the result. Assignment itself does not imply a deep copy.
Line 5: alias.append(3)
Call alias.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.
Line 6: print(original, alias is original)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: separate = original.copy()
Evaluate original.copy(), then bind or update separate with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Line 8: separate.append(4)
Call separate.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.
Line 9: print(original, separate)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 10: nested = [[1], [2]]
Evaluate [[1], [2]], then bind or update nested with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 11: shallow = nested.copy()
Evaluate nested.copy(), then bind or update shallow with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Line 12: deep = deepcopy(nested)
Evaluate deepcopy(nested), then bind or update deep with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Line 13: shallow[0].append(9)
Call shallow[0].append to add one item to the existing list. This call may have an effect even when its return value is not assigned.
Line 14: print("Original:", nested, "Deep copy:", deep)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 15: missing = None
Evaluate None, then bind or update missing with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 16: print(missing is None)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Do not use is for ordinary string or numeric equality. Implementation caching can make such comparisons appear to work unpredictably.