Monday, July 6, 2015

Differences between Hard Copy, shallow copy and Deep Copy in python




Assignment Operation is called as Hard Copy.

In [1]: par_list=[1,2,3,4,5]

In [2]: child_list=par_list   # Simple Assignment is called HardCopy

In [3]: par_list
Out[3]: [1, 2, 3, 4, 5]

In [4]: child_list
Out[4]: [1, 2, 3, 4, 5]

In [5]: # 'is' operator returns True if both are addressing to the same object

In [6]: # '==' operator checks the equivalence of the Values only

In [7]: par_list == child_list
Out[7]: True

In [8]: par_list is child_list
Out[8]: True

In [9]: par_list
Out[9]: [1, 2, 3, 4, 5]

In [10]: par_list[2] = 'Udhay'

In [11]: par_list
Out[11]: [1, 2, 'Udhay', 4, 5]

In [12]: child_list
Out[12]: [1, 2, 'Udhay', 4, 5]

In [13]: child_list[0] = "Prakash"

In [14]: child_list
Out[14]: ['Prakash', 2, 'Udhay', 4, 5]

In [15]: par_list
Out[15]: ['Prakash', 2, 'Udhay', 4, 5]

In [16]: # As they are pointing to the same object, changing any of them, is reflect

In [17]: # Now, let us compare this Hard COPY with Shallow COPY and Deep COPY

In [18]: import copy

In [19]: #shallow copy ----copy.copy()

In [20]: #Deepcopy=========copy.deepcopy()

In [21]: # Let us reassign the values

In [22]: par_list=[1,2,3,4,5]

In [23]: child_list1 = par_list

In [24]: child_list2=copy.copy(par_list)

In [25]: child_list3=copy.deepcopy(par_list)

In [26]: par_list==child_list1==child_list2==child_list3
Out[26]: True

In [27]: par_list is child_list1 is child_list2 is child_list3
Out[27]: False

In [28]: par_list is child_list1
Out[28]: True

In [29]: (par_list is child_list1, par_list is child_list2, par_list is child_list3)
Out[29]: (True, False, False)

In [30]: help(id)
Help on built-in function id in module __builtin__:

id(...)
    id(object) -> integer

    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)


In [31]: (id(par_list), id(child_list1), id(child_list2), id(child_list3))
Out[31]: (51084984, 51084984, 51099888, 51084824)

In [32]: par_list
Out[32]: [1, 2, 3, 4, 5]

In [33]: par_list[3] = "new"

In [34]: par_list
Out[34]: [1, 2, 3, 'new', 5]

In [35]: par_list, child_list1, child_list2, child_list3
Out[35]: ([1, 2, 3, 'new', 5], [1, 2, 3, 'new', 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]

In [36]: # Only HardCOPYed list is effected

In [37]: # It is an advantage in some cases.

In [38]: # To investigate the difference between shallow copy and deepcopy, get us g

In [39]: par_list=[1,2,3,[4,5,[6,7]]]

In [40]: par_list
Out[40]: [1, 2, 3, [4, 5, [6, 7]]]

In [41]: child_list2=copy.copy(par_list) #shallow COPY

In [42]: child_list3=copy.deepcopy(par_list) #Deep COPY

In [43]: par_list
Out[43]: [1, 2, 3, [4, 5, [6, 7]]]

In [44]: child_list2
Out[44]: [1, 2, 3, [4, 5, [6, 7]]]

In [45]: child_list3   #Deep COPY
Out[45]: [1, 2, 3, [4, 5, [6, 7]]]

In [46]: par_list is child_list2 #shallow COPY
Out[46]: False

In [47]: par_list is child_list3 #Deep COPY
Out[47]: False

In [48]: par_list == child_list2 #shallow COPY
Out[48]: True

In [49]: par_list == child_list3 #Deep COPY
Out[49]: True

In [50]: par_list[3][2][1]
Out[50]: 7

In [51]: par_list[3][2][1] = "seven"

In [52]: par_list
Out[52]: [1, 2, 3, [4, 5, [6, 'seven']]]

In [53]: child_list2   #shallow COPY
Out[53]: [1, 2, 3, [4, 5, [6, 'seven']]]

In [54]: child_list3   #Deep COPY
Out[54]: [1, 2, 3, [4, 5, [6, 7]]]

Verifying with single dimension lists
In [59]: par_list = [1,2,3,4]

In [60]: pa
%page     par_list  pass      %paste    %pastebin

In [60]: child_list1 = par_list

In [61]: child_list2 = copy.copy(par_list)

In [62]: child_list3 = copy.deepcopy(par_list)

In [63]: par_list[2]= "two"

In [64]: par_list
Out[64]: [1, 2, 'two', 4]

In [65]: child_list1
Out[65]: [1, 2, 'two', 4]

In [66]: child_list2
Out[66]: [1, 2, 3, 4]

In [67]: child_list3
Out[67]: [1, 2, 3, 4]

CONCLUSION: In single dimension lists, if you do not want the copied list not to get affected to the changes in source list, go for shallow COPY.
 In multi-dimension lists, if you do not want the copied list not to get affected to the changes in source list, go for Deep COPY.


click here for video tutorial