What It Is
The Prototype pattern creates new objects by cloning an existing one instead of building from zero. You prepare one object as a template, then copy it and change only what differs.
The one thing to get right in Python: a shallow copy (copy.copy) shares the lists and dicts inside an object — copy.deepcopy copies them too, so the clone is truly independent.
Example: A Document Template
The template carries the parts every document repeats. clone() uses copy.deepcopy — the tags list included — so editing the copy leaves the template untouched.
Clone, then change the difference
The report got a new title and an extra tag, and the template still has only ['draft'] — because copy.deepcopy copied the tags list. A shallow copy.copy would have shared it, and the template would have changed too.
Output: complete Python program
import copy
from dataclasses import dataclass, field
@dataclass
class Document:
title: str
footer: str
tags: list[str] = field(default_factory=list)
# clone copies the document — the tags list included.
def clone(self) -> "Document":
return copy.deepcopy(self)
template = Document(
title="Untitled",
footer="PromptCookbook 2026",
tags=["draft"],
)
report = template.clone()
report.title = "Yearly Report"
report.tags.append("finance")
print("template:", template.title, template.tags)
print("clone: ", report.title, report.tags)
When to Use It
Use it when new objects are mostly copies of a prepared one — documents from a template, game entities from a spawner, test data from a fixture.
Skip it when objects are cheap to build fresh, or contain nothing shared. For a flat object of plain values, a shallow copy.copy is already a full copy.
Pros and Cons
Pros: cheap creation of preconfigured objects; the template documents the defaults; no repeated setup code.
Cons: copying is easy to get subtly wrong — a shallow copy in the wrong place quietly links clone and original through a shared list or dict; clones drift from the template silently over time.