Prototype Design Pattern in Python…

The prototype design pattern is a creational pattern.

If the creation of an object is a very costly affair and we already have a similar kind of object, instead of creating it each time we need it, we use the clone of the object that is already there….


This content originally appeared on DEV Community and was authored by Somenath Mukhopadhyay

The prototype design pattern is a creational pattern.

If the creation of an object is a very costly affair and we already have a similar kind of object, instead of creating it each time we need it, we use the clone of the object that is already there.

Suppose, there is a large object with many attributes whose data we read from a database. We need to modify that data multiple times in our program. So instead of creating the object each time by reading from the database, we tend to keep a prototype object - we clone it, and then work on it.

In Python, there is a module called copy which has two methods - copy() and deepcopy(). The first one is for the shallow copy and the second one is for the deepcopy.

Here is the source code of an example of the Prototype design pattern.

```
from abc import ABC, abstractmethod  
import copy  

class Person(ABC):  
    def __init__(self, name):  
        self._name = name  
    def set_name(self, name):  
        self._name = name  
    def get_name(self):  
        return self._name  
    @abstractmethod  
    def clone(self):  
        pass  
    @abstractmethod  
    def display(self):  
        pass  

class Teacher(Person):  
    def __init__(self, name, course):  
        super().__init__(name)  
        self._course = course  
    def set_course(self, course):  
        self._course = course  
    def get_course(self):  
        return self._course  
    def display(self):  
        print("\nTeacher's name : ", self._name,  "\n")  
        print("\nHe teaches : ", self.get_course())  

    def clone(self):  
        return copy.deepcopy(self)  

class Student(Person):  
    def __init__(self, name, teacher):  
        super().__init__(name)  
        self._teacher = teacher  

    def  display(self):  
        print("\n Student's name : ", self.get_name())  
        print("\n Taught by : ", self._teacher.get_name())  
        print("\n Enrolled in the subject :  ", self._teacher.get_course())  

    def clone(self):  
        return copy.deepcopy(self)  

if __name__ == '__main__':  
    teacher = Teacher('Som', "Python Design Patttern")  
    teacherClone1 = teacher.clone()  

    student = Student ("Ridit", teacherClone1)  

    studentClone1 = student.clone()  

    teacherClone1.set_course("DSA")  

    teacherClone1.display()  

    studentClone1.display()  

 ```

If we run the above piece of code the result will be as follows:

Teacher's name : Som

He teaches : Python Design Patttern

Teacher's name : Som

He teaches : DSA

Student's name : Ridit

Taught by : Som

Enrolled in the subject : Python Design Patttern

As you can see, we have used deep copy here.


This content originally appeared on DEV Community and was authored by Somenath Mukhopadhyay


Print Share Comment Cite Upload Translate Updates
APA

Somenath Mukhopadhyay | Sciencx (2025-09-05T06:11:19+00:00) Prototype Design Pattern in Python…. Retrieved from https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/

MLA
" » Prototype Design Pattern in Python…." Somenath Mukhopadhyay | Sciencx - Friday September 5, 2025, https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/
HARVARD
Somenath Mukhopadhyay | Sciencx Friday September 5, 2025 » Prototype Design Pattern in Python…., viewed ,<https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/>
VANCOUVER
Somenath Mukhopadhyay | Sciencx - » Prototype Design Pattern in Python…. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/
CHICAGO
" » Prototype Design Pattern in Python…." Somenath Mukhopadhyay | Sciencx - Accessed . https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/
IEEE
" » Prototype Design Pattern in Python…." Somenath Mukhopadhyay | Sciencx [Online]. Available: https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/. [Accessed: ]
rf:citation
» Prototype Design Pattern in Python… | Somenath Mukhopadhyay | Sciencx | https://www.scien.cx/2025/09/05/prototype-design-pattern-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.