-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering Python Design Patterns
By :

The prototype pattern allows you to create new objects by copying existing ones, rather than creating them from scratch. This pattern is particularly useful when the cost of initializing an object is more expensive or complex than copying an existing one. In essence, the prototype pattern enables you to create a new instance of a class by duplicating an existing instance, thereby avoiding the overhead of initializing a new object.
In its simplest version, this pattern is just a clone()
function that accepts an object as an input parameter and returns a clone of it. In Python, this can be done using the copy.deepcopy()
function.
Cloning a plant by taking a cutting is a real-world example of the prototype pattern. Using this approach, you don’t grow the plant from a seed; you create a new plant that’s a copy of an existing one.
Many Python applications make use of the prototype pattern, but it is rarely referred to as...