Copy in Python

In python programs, several times we need to have an identical copy of existing data. For simple data types like int, float, boolean values or string, an assignment operation does the task for us as they are immutable data types. After copying, when we make any changes to any variable with immutable data types, a new instance of data is created and they don’t affect the original data. In case of mutable data types like lists or dictionaries, If we use an assignment operator to copy the data to another variable, both the variables refer to the same data object and if we make changes to any of the variables, the object gets changed reflecting the effect on both the variables. In this article, we will try to understand this concept of copy in python with examples.

Read Also: Java interview questions

How to get the object ID of an object in python?

To illustrate the concept of copy in python, we will need to know the object ID of an object. For this task, we will use the id() function. This function takes the variable name as input and returns a unique id for the object. This can be seen from the following example.

var1=1
print("ID of object at which var1 refers:",end=" ")
print(id(var1))

Output:

ID of object at which var1 refers: 9784896

Copy an object using = operator in python

When we try to copy an object using = operator by assigning a variable to another, it doesn’t create a new object but both the variables are assigned to the same object.

For example, if we have a variable named var1 referring to an object and we assign var1 to var2 by the statement var2=var1, both the variables point to the same object and thus will have the same ID. This can be seen as follows.

var1=11
var2=var1
print("ID of object at which var1 refers:",end=" ")
print(id(var1))
print("ID of object at which var2 refers:", end=" ")
print(id(var2))

Output:

ID of object at which var1 refers: 9785216
ID of object at which var2 refers: 9785216

For immutable objects, when we change the value assigned to any of var1 or var2, a new object is created for the newly assigned value to the variable. So, if we change the value assigned to var2, a new object will be created and after reassignment, var1 and var2 will refer to objects with different object ID. This can be understood as follows.

Copy an object using = operator in python

var1=11
var2=var1
print("ID of object at which var1 refers:",end=" ")
print(id(var1))
print("ID of object at which var2 refers:", end=" ")
print(id(var2))
print("After Assigning value to var2")
var2=10
print("ID of object at which var1 refers:",end=" ")
print(id(var1))
print("ID of object at which var2 refers:", end=" ")
print(id(var2))

Output:
Copy an object using = operator in python 1

ID of object at which var1 refers: 9785216
ID of object at which var2 refers: 9785216
After Assigning value to var2
ID of object at which var1 refers: 9785216
ID of object at which var2 refers: 9785184

Unlike above, if we are copying mutable objects like lists, they point to the same object after reassignment and modification using any variable leads to change in the same object. This can be seen as follows.

list1=[1,2,3]
list2=list1
print("ID of object at which list1 refers:",end=" ")
print(id(list1))
print("ID of object at which list2 refers:", end=" ")
print(id(list2))
print("After appending another value to list2")
list2.append(4)
print("ID of object at which var1 refers:",end=" ")
print(id(list1))
print("ID of object at which var2 refers:", end=" ")
print(id(list2))

Output:

ID of object at which list1 refers: 140076637998720
ID of object at which list2 refers: 140076637998720
After appending another value to list2
ID of object at which var1 refers: 140076637998720
ID of object at which var2 refers: 140076637998720

In the above output, we can see that when we use an assignment operator to copy list, both the variables list1 and list2 refer to same object and have same ID. This doesn’t change when we make changes to any of the list.

How to copy mutable objects in Python?

For copying mutable objects like lists or dictionaries, we use copy() method.

When invoked on any object, the copy() method creates a new object with the same data as the original object and returns a reference to it. It means that when we copy  the objects using the copy() method instead of = operator, the address of the original object and copied object are different.

For example if we copy list1 to list2 using the copy() method then list1 and list2 will refer to different objects. This can be seen as follows.
How to copy mutable objects in Python 2

list1=[1,2,3]
list2=list1.copy()
print("ID of object at which list1 refers:",end=" ")
print(id(list1))
print("ID of object at which list2 refers:", end=" ")
print(id(list2))

print("After appending another value to list2")
list2.append(4)
print("ID of object at which var1 refers:",end=" ")
print(id(list1))
print("ID of object at which var2 refers:", end=" ")
print(id(list2))

Output:
How to copy mutable objects in Python 1

ID of object at which list1 refers: 140076492253376
ID of object at which list2 refers: 140076483798272
After appending another value to list2
ID of object at which var1 refers: 140076492253376
ID of object at which var2 refers: 140076483798272

In the output, It can be seen that list2 has a different object ID than list1. If we modify any values in list1, it will not cause any change to list2 which is not the case when we use = operator to copy an object to another.

Conclusion

In this article, we have studied about how to copy mutable and immutable objects in python. For immutable objects, we can use = operator because at each reassignment, a new object is created for immutable objects. In case of mutable objects like python dictionary, we should use the copy() method to copy an object like a list or dictionary to avoid unwanted errors in the program.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *