Difference between Shallow Copy and Deep Copy - LetsProgram

Share:
Before discussing the types of copying first we have to discuss the memory occupied by the datatypes in the system. We know that integer occupies 4 bytes, string occupies 4 bytes and float occupies 8 bytes. But what will python do when we have the same value stored with different variables will it allocate two spaces in the memory. id(variable_name) will give the number allocated to memory in the system.

>>>x = 3
>>>y = x
>>>print(id(x),id(y))
140709828469200 140709828469200

The output id's of x and y are the same which means x and y are allocated in the same space. Python not created a different memory location for the new variable y which has the same value as x.However, unlike "real" pointer like those in C and C++, things change, when we assign a new value to y. In these cases, y will receive a separate memory location.

In python, assignment operations will not create a new object but they create a link between the variable and the object.  When = operation is used user thinks a new object is created but it only creates a new variable that shares the reference of the original object. Sometimes, the user will work with the list (mutable objects) he will do many operations to it. sometimes a user wants copies that users can modify without automatically modifying the original at the same time, in order to do that we create copies of objects.

This module does not copy types like module, method, stack trace, the stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged;

In python, there are two ways to copy.

  • Deep Copy
  • Shallow Copy

Deep Copy


In a deep copy, an object is copied into another object. It means that any changes made to the copy of the object are not observed in the original object. In python deep copy is done by using deepcopy() method from the copy module.

>>>copy.deepcopy(x[,memo])

will return the deep copy of x

>>>import copy
>>>list1 = [ 1,2,3]
>>>list2 = copy.deepcopy(list1)
>>>print(id(list1) , id(list2))
2638568131392 2638561816448

A deep copy constructs a new object and then insert the copies of 
original into the new object recursively.

Deepcopy


Two problems often exist with deep copy operations that don’t exist with shallow 
copy operations:

  • Recursive objects (compound objects that, directly or indirectly, contain a      reference to themselves) may cause a recursive loop.
  • Because deep copy copies everything it may copy too much, such as data that is intended to be shared between copies.

The deepcopy() function avoids these problems by:

  • keeping a memo dictionary of objects already copied during the current copying pass; and
  • letting user-defined classes override the copying operation or the set of components copied
When we change the copy of the original object there will be no change in the original object.



Shallow Copy

A shallow copy means creating a new object and connecting the references with the original object. The copying process does not recurse and therefore won’t create copies of the child objects themselves. In the case of shallow copy, a reference of the object is copied in other objects. It means that any changes made to a copy of the object do reflect in the original object. In python, this is implemented using the “copy()” function.

>>>copy.copy(x)

This will return the shallow copy of x

>>>import copy
>>>list1 = [1,2,3,4,5]
>>>list2 = copy.copy(list1)
>>> print(id(list1),id(list2))
3027379129216 3027372888128

Shallowcopy

Even both the original and new objects are referring to the same values but the id's of both objects are different. This tells us both the objects are occupying different spaces in the memory.



The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

No comments

F