Python Design Patterns

Prototype Pattern in Python

Copy an existing, configured object and change a few fields instead of building a new one from scratch.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • A clone() method built on copy.copy
  • Shallow versus deep copies
  • When cloning beats constructing
published = draft.clone()
original
  • status: Draft
clone
  • 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.

From photocopying a form to code
draft = Document("Draft")📝 The original, filled-in form
draft.clone()📠 Make a photocopy
published.status = "Published"✏️ Change one field on the copy
draft.status📄 The original still says Draft
Clone an existing object, then adjust the copy.
In one sentence

The Prototype pattern creates new objects by copying an existing object (the prototype) and then changing only what is different.

Key points

  1. clone() returns copy(self), a new object with the same attribute values.
  2. published is a separate object, so changing published.status does not affect draft.
  3. copy makes a shallow copy: nested lists or dictionaries would still be shared.
  4. 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

Run code →
main.py
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)
Get a Run button for this example

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.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Using a shallow copy for nested data

copy() shares inner lists, so changing the copy's list also changes the original.

Problem
from copy import copy
clone = copy(document)  # document.tags is shared
Fix
from copy import deepcopy
clone = deepcopy(document)

Finished this lesson?

Mark it as learned to track your progress.