Open In App

Copy Constructor in Python

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented programming, a copy constructor is a special type of constructor that creates a new object as a copy of an existing object. It is a crucial concept in many programming languages, including Python. Python, being an object-oriented language, supports the implementation of copy constructors to enable the creation of new objects by copying the attributes of existing ones. In this article, we will explore the concept of copy constructors in Python, understand their types, and provide examples to illustrate their usage.

What is Copy Constructor In Python?

A copy constructor is a method that initializes a new object using the attributes of an existing object. In Python, the __init__ method serves as a constructor, and a copy constructor can be implemented by defining a method with a specific signature. The purpose of a copy constructor is to create a deep or shallow copy of an object, depending on the specific requirements.

Copy Constructor Types in Python with Example

There, are two types of Copy constructors in Python, those are following.

1. Shallow Copy Constructor

A shallow copy creates a new object, but instead of copying the elements of the original object, it copies references to the objects. This means that changes made to the nested objects in the copied object will be reflected in the original object and vice versa. In Python, the copy module provides the copy() method to create shallow copies.

Python3




import copy
 
class ShallowCopyExample:
    def __init__(self, data):
        self.data = data
 
    def __str__(self):
        return f"Original Object: {self.data}"
 
    def shallow_copy(self):
        return copy.copy(self)
 
# Example usage
original_object = ShallowCopyExample([1, 2, 3])
copy_object = original_object.shallow_copy()
 
print(original_object)
print(copy_object)
 
# Modify the copied object
copy_object.data.append(4)
 
# Changes reflected in the original object
print(original_object)
print(copy_object)


Output

Original Object: [1, 2, 3]
Original Object: [1, 2, 3]
Original Object: [1, 2, 3, 4]
Original Object: [1, 2, 3, 4]


2. Deep Copy Constructor

A deep copy creates a new object and recursively copies all nested objects, ensuring that changes made in the copied object do not affect the original object. Python’s copy module also provides the deepcopy() method for creating deep copies

Python3




import copy
 
class DeepCopyExample:
    def __init__(self, data):
        self.data = data
 
    def __str__(self):
        return f"Original Object: {self.data}"
 
    def deep_copy(self):
        return copy.deepcopy(self)
 
# Example usage
original_object = DeepCopyExample([1, [2, 3], 4])
copy_object = original_object.deep_copy()
 
print(original_object)
print(copy_object)
 
# Modify the copied object
copy_object.data[1].append(99)
 
# Changes not reflected in the original object
print(original_object)
print(copy_object)


Output

Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3], 4]
Original Object: [1, [2, 3, 99], 4]


Conclusion

Understanding copy constructors in Python is essential for effective object-oriented programming. Whether creating shallow copies for simple objects or deep copies for more complex structures, copy constructors provide a convenient way to duplicate objects without the risk of unintended side effects. The copy module in Python simplifies the implementation of both shallow and deep copy constructors, contributing to the language’s flexibility and versatility in handling object copies.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads