Jan 22, 2019

Python Shallow Vs Deep Copy

import copy

#Shallow Copy
#   copy.copy(x)
#   A shallow copy creates a new object which stores the reference of the original elements.
#   A shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. 

#Deep Copy
#   copy.deepcopy(x)
#   A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements.


print ' --------- Copy by Ref ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = dic1

print dic1
print dic2

print 'Updating/Appending score 50'
dic1['Scores'].append(50) #update existing

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

#Both dic1 & dic2 point to same memory locations
print id(dic1) #46138688
print id(dic2) #46138688

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

print ' --------- Shallow Copy ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = copy.copy(dic1)

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print "Memory locations of dic1 & dic2 respectively"
#Both dic1 & dic2 point to different memory location
print id(dic1) #57641696
print id(dic2) #57641840

print 'Updating/Appending score 50'
#Updating the existing will 
dic1['Scores'].append(50) #update existing

#Both dic1 & dic2 point to different memory locations
print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

#Shallow Copy -> Internal data points to the same memory location
print "Memory locations of dic1['Scores'] & dic2['Scores'] respectively"
print id(dic1['Scores']) #52223472
print id(dic2['Scores']) #52223472

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

print ' --------- Deep Copy ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = copy.deepcopy(dic1)

#Both dic1 & dic2 point to different memory locations
print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print id(dic1) #54035776
print id(dic2) #54037504

print 'Updating/Appending score 50'
dic1['Scores'].append(50) #update existing

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

No comments:

Post a Comment