Why did changing a copied Python request alter the original?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
Assignment creates another binding to the same object. A shallow copy makes a new outer container but preserves references to nested objects, which matters when requests share mutable metadata.
Two document jobs share a nested tags list after one request dictionary is copied with dict.copy().
from copy import deepcopy
original = {"tags": ["draft"]}
shallow = original.copy()
isolated = deepcopy(original)
shallow["tags"].append("shared")
isolated["tags"].append("private")
print(original["tags"])
print(shallow["tags"])
print(isolated["tags"])Show expected output
['draft', 'shared']
['draft', 'shared']
['draft', 'private']Copy and paste to run in a separate tab. This example illustrates one concept; it is not a complete application or an automatically graded task.
A practical way to learn it
Run the copy example, predict all three printed lists, then change the code to isolate tags before adding a new tag.
Open Python playgroundThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would draw the object references before changing the code. A shallow dictionary copy isolates assignments to top-level keys, but a nested list can still be shared. I would copy the specific mutable path I intend to change or use a deliberately chosen deep copy when the object supports it and the cost is appropriate. I would avoid a blanket deepcopy of clients, connections or very large payloads. I would also check mutable default arguments and reused templates, then test that concurrent jobs cannot alter each other's request metadata.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Assuming dict.copy() recursively isolates every nested value.
Now go one level deeper
- Why can a mutable function default leak state between calls?
- When would copying an entire payload be unnecessarily expensive?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.