Python copy Module

If your a beginner in Python Programming then you may not be aware of how much copying of variables/functions is needed to write large scale programs. Moreover sometimes two different variables need to point to same memory location object or other times different variables need to have same value but stored at different locations in memory.
For implementing this Python’s copy Module provides copy() and deepcopy() functions. Let’s discuss what are copy(), deepcopy() functions.

  • copy() => This function constructs a shallow copy which is a new compound object and then inserts references into it to objects found in original collection object
  • deepcopy() => This function constructs a deep copy which is a new compound object and then recursively insert copies into it of objects found in original collection object.

Do note that difference between shallow and deep copying is only visible if objects which are getting copied are Compound Objects like Lists or Classes.

Deep Copying in Python

Dopy Copying means firstly allocate memory for collection object and then recursively fill that newly allocated memory with copies of objects in the original collection object. As for newer copy of object new memory is being allocated, that’s why making changes to original object would not be visible in copied child object or vice-versa.
See below diagram showing Different parts of memory being allocated for Original and Child Objects.

Deep Copying in Python Programming Language

Python Code for Doing Deep Copying of Objects

Deep copies of objects can be created using copy module’s deepcopy() function.
Syntax of deepcopy() Function => copy.deepcopy(object)

import copy

old_list = [[1,2,3], [4,5,6], [7,8,9]]
new_list = copy.deepcopy(old_list)

print("Old list is => ", old_list)         # Prints out [[1,2,3], [4,5,6], [7,8,9]]
print("New list is => ", new_list)         # Prints out [[1,2,3], [4,5,6], [7,8,9]]

# Let's add [28,193, 30] to old_list
old_list.append([28,193, 30])

# Let's now see Whether old_list and new_list are still same
print("Now old list is => ", old_list)      # Prints out [[1,2,3], [4,5,6], [7,8,9],[28,193, 30]]
print("New list is => ", new_list)          # Prints out [[1,2,3], [4,5,6], [7,8,9]]

In the above, you can clearly see that despite updating old_list new_list is not changing. Which means two objects old_list and new_list have same values but are stored differently in Memory. As Python allocates unique id to every object in program, that’s why you check whether id of old_list, new_list to verify whether these are two different memory locations or not.

import copy

old_list = [[1,2,3], [4,5,6], [7,8,9]]
new_list = copy.deepcopy(old_list)

print("Old list is => ", old_list)         # Prints out [[1,2,3], [4,5,6], [7,8,9]]
print("New list is => ", new_list)         # Prints out [[1,2,3], [4,5,6], [7,8,9]]

print("Memory Location of Old List => ", old_list)           # Prints out 140435995792960
print("Memory Location of new list => ", new_list)           # Prints out 140435995793600

Shallow Copying in Python

Shallow copying means constructing a new collection object and then populating it with references to the child objects found in the original collection object. This process doesn’t create newer objects rather just create reference to already existing object. As newer copy of object and original object internally point to same memory location. That’s why changing original object means copied version will also change accordingly.

Python Code for Doing Shallow Copying of Objects

>>> import copy
>>> xs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zs = copy.deepcopy(xs)

>>> xs
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zs
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# In a deep copy you can change any element without changing the
original, since you created new objects instead of using 
references to the original:

>>> xs[1][0] = 'X'
>>> xs
[[1, 2, 3], ['X', 5, 6], [7, 8, 9]]
>>> zs
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts