Prototype Pattern in Python
Copy an existing, configured object and change a few fields instead of building a new one from scratch.
- A clone() method built on copy.copy
- Shallow versus deep copies
- When cloning beats constructing
- status: Draft
- status: Published
- changed independently
Clone, then adjust
The problem
Sometimes creating an object from scratch is slow or complicated, but you already have one that is almost right: a configured document, a game enemy with tuned settings. Copying it and changing a few fields is simpler than building another.
Think of it like this
The Prototype pattern is like photocopying a filled-in form. Instead of writing everything again, you copy the form and change only the fields that differ. The original stays exactly as it was.
draft = Document("Draft")📝 The original, filled-in formdraft.clone()📠 Make a photocopypublished.status = "Published"✏️ Change one field on the copydraft.status📄 The original still says DraftThe Prototype pattern creates new objects by copying an existing object (the prototype) and then changing only what is different.
Key points
clone()returnscopy(self), a new object with the same attribute values.publishedis a separate object, so changingpublished.statusdoes not affectdraft.copymakes a shallow copy: nested lists or dictionaries would still be shared.- The output shows Draft for the original and Published for the copy.
When should you use it?
- Objects that are expensive or complicated to set up, copied and then adjusted.
- Templates such as documents, game units or configurations.
- Saving a starting state to create many variants from it.
When not to use it
- Objects that are cheap to create from scratch: just call the constructor.
- Objects holding resources such as open files or connections, which should not be copied.
Example code
from copy import copy
# Prototype: clone an existing configured object.
class Document:
def __init__(self, status):
self.status = status
def clone(self):
# Return a shallow copy of this configured object.
return copy(self)
draft = Document("Draft")
published = draft.clone()
published.status = "Published"
print(draft.status)
print(published.status)
Result of running the code
Draft
Published
Code explanation
clone() uses copy() to create a new Document with the same attribute values as the one it is called on. draft and published are therefore two different objects, and setting published.status to "Published" leaves draft untouched. Because copy() is shallow, attributes that hold lists or dictionaries would still point to the same inner objects.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Using a shallow copy for nested data
copy() shares inner lists, so changing the copy's list also changes the original.
from copy import copy
clone = copy(document) # document.tags is shared
from copy import deepcopy
clone = deepcopy(document)
Finished this lesson?
Mark it as learned to track your progress.