Saturday, 10 June 2017

Hard copy and Shallow copy and Deep copy

>>> import copy

>>> par_list=[1,2,3]

>>> c1_list=par_list

>>> c2_list=copy.copy(par_list)

>>> c3_list=copy.deepcopy(par_list)

>>> par_list==c1_list
True

>>> par_list is c1_list
True

>>> par_list == c2_list
True

>>> par_list is c2_list
False

>>> par_list ==c3_list
True

>>> par_list is c3_list
False

>>> par_list
[1, 2, 3]

>>> par_list[1]='siva'

>>> par_list
[1, 'siva', 3]

>>> c1_list
[1, 'siva', 3]

>>> c2_list
[1, 2, 3]

>>> c3_list
[1, 2, 3]

>>> c1_list[1]=3+4j

>>> c1_list
[1, (3+4j), 3]

>>> par_list
[1, (3+4j), 3]

>>> c2_list
[1, 2, 3]

>>> c3_list
[1, 2, 3]

>>> c2_list[1]=2.3

>>> c2_list
[1, 2.3, 3]

>>> par_list
[1, (3+4j), 3]

>>> c1_list
[1, (3+4j), 3]

>>> c3_list
[1, 2, 3]

>>> c3_list[1]='krishna'

>>> c3_list
[1, 'krishna', 3]

>>> par_list
[1, (3+4j), 3]

>>> c1_list
[1, (3+4j), 3]

>>> c2_list
[1, 2.3, 3]

#multi dimensional list

>>> p_list=[1,2,3,[4,5,[6,7]]]

>>> ch1_list=p_list

>>> ch1_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> ch2_list=copy.copy(p_list)

>>> ch2_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> ch3_list=copy.deepcopy(p_list)

>>> ch3_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> p_list==ch1_list==ch2_list==ch3_list
True

>>> p_list is ch1_list,p_list is ch2_list,p_list is ch3_list
(True, False, False)


>>> p_list[3][2][1]
7

>>> p_list[3][2][1]='siva'

>>> p_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch1_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch2_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch3_list
[1, 2, 3, [4, 5, [6, 7]]]
>>> 

No comments:

Post a Comment