We will see How to clone an object in Python, In Python, you can copy an object using the copy module or by using the dict attribute of the object.
Using the Copy Module:
Example:
import copy
# Create the original object
original_obj = {'a': 1, 'b': 2}
# Clone the object
cloned_obj = copy.deepcopy(original_obj)
# Modify the original object
original_obj['a'] = 3
# Print both objects
print(original_obj) # Output: {'a': 3, 'b': 2}
print(cloned_obj) # Output: {'a': 1, 'b': 2}
Output:

The deepcopy() function in the copy module produces a new object with a new memory address and copies all of the original object’s attributes to the new object.
Using the dict attribute:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}, {self.age}"
# Create the original object
person1 = Person("John", 30)
# Clone the object
person2 = Person(**person1.__dict__)
# Modify the original object
person1.age = 40
# Print both objects
print(person1) # Output: John, 40
print(person2) # Output: John, 30
Output:

An object’s dict attribute can be used to duplicate it. The dict attribute is a dictionary that includes each of an object’s instance attributes.
In this example, we generated a Person class with attributes for name and age. We then built an original object person1 named “Abhishek” who was 30 years old. This object was cloned by creating a new object person2 and using the dict attribute of person1. We then changed person1’s age property to 40 and printed both objects to ensure that the cloning was successful.