Prototype: copy a configured starting point
Picture
A report template has a title and a list of columns. A new report should inherit those choices but be editable independently.
Predict
Does an outer dictionary copy always copy nested lists?
Walk through the small example
No. Shallow copying preserves references to nested objects.
Mental model and complete Python solution
Prototype creates a new instance from an already configured one. The meaningful decision is which state is copied, shared, or recreated. deepcopy suits this simple data-only template, but should not be used blindly for identities, connection pools, locks, or network sessions.
What the function promises: clone_report returns a deep copy of a data-only template with a new title.
from copy import deepcopy
def clone_report(template, title):
result = deepcopy(template)
result["title"] = title
return result
template = {"title": "Base", "columns": ["name", "score"]}
report = clone_report(template, "Today")
report["columns"].append("rank")
assert template["columns"] == ["name", "score"]
assert report["title"] == "Today"
Work and storage: O(copied object-graph size) time and storage.
When to recognize it: Deep copying works for this data-only example. Connections, locks, and external resources need an explicit reconstruction policy.
Check before applying it: Cloning needs an ownership policy, especially for identity and external resources.
Change one rule
The template contains a database connection. Should cloning copy it?
See what changes
No. Copy configuration and ask the connection owner or factory for an appropriate resource. Resource lifetime is not ordinary nested data.
Connect
Builder: assemble a valid object: Build a fresh configuration or clone an existing valid one.
Objects and responsibilities: Each new object needs a clear boundary for independently owned state.