Skip to main content

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

Comments

Popular posts from this blog

NSG 2.1 Tcl/OTcl Script Generator

Are you a beginner for ns2 network simulator? Are you afraid of Tcl/oTcl script generation? Then there is a tcl script generator, named NSG 2.1 NSG - Network Simulation Generator NSG 2.1 is a java .jar file.  So, this application can run on all platforms (windows/linux/mac os). It deserves the java installed in you pc, prior to working with NSG 2.1. Java must be installed to run NSG2.1. So, initially, java must be installed. How to install java in Windows/ubuntu/mint/debian linux/ OS X ? Step 1 : Go to Terminal and run  java -version   to check the java version installed in your machine. Step 2 :  For Windows, click here  to download the java installer. Then, it is a typical next-next windows executable installation. For ubuntu/mint/debian linux operating systems, run the following commands in that terminal: sudo apt-get install default-jre sudo apt-get install default-jdk sudo apt-get install openjdk-7-jre sudo apt-get install openjdk-7- jdk How to

SUMO installation in linux (Debian/Ubuntu/Mint)

SUMO - S imulation of U rban Mo bility SUMO an open source, portal, microscopic, multi-modal road traffic simulation. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.  It allows to simulate as to how a given traffic demand which consists of single vehicles moves through a given road network. The simulator allows to address a large set of traffic management topics. It is purely microscopic: each vehicle is modeled explicitly, has an own route, and moves individually through the network. SUMO Installation Procedure: Installation in Linux and mac OS Step I: Install two pre-requisite packages to build SUMO with GUI. Go to terminal and run: $ sudo apt-get install libgdal1-dev proj libxerces-c2-dev $ sudo apt-get install libfox-1.6-dev libgl1-mesa-dev libglu1-mesa-dev Step II: If you are using Unbuntu 12.04 or older versions, as it doesn't ship with libgdal package, create a symbolic link: $ sudo ln -s /usr/lib/li

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len