Python - After a variable is assigned to another variable, under what condition do we assume the concept of shallow / deep copy? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python - After a variable is assigned to another variable, under what condition do we assume the concept of shallow / deep copy?

I'm comparing three chunks of code: Case 1: ====== s = "string" x = s s= s+"s" print(s) #outputs strings print(x) #outputs string --> would x be a deep copy of s because concatenation was involved? Case 2: ====== c = [4,5,6] d = c c = c + ["KKK"] print(c) #outputs [4, 5, 6, 'KKK'] print(d) #outputs [4, 5, 6] --> would d be a deep copy of c because appending was involved? Case 3: ====== a = [1,2,3] b = a a[0] = "zzz" print(a) #outputs ['zzz', 2, 3] print(b) #outputs ['zzz', 2, 3] --> would b be a shallow copy of a because of the modification of a list element? I am having trouble distinguishing cases of shallow vs deep copy. From my current understanding: i) variable1 and a variable2 that is assigned variable1 are COPIES OF EACH OTHER ii) shallow copy: changes to one copy DO AFFECT the other copy iii) deep copy: changes to one copy do NOT affect the other copy Referring to the 3 cases above, case 1 and case 2 appears to necessitate the the deep copy concept, while case 3 the shallow copy concept. Does the specific WAY of variable modification determine the copy type? --> If so, under what condition do we assume the concept of shallow / deep copy? Or have I forgotten something important and complicating matters?

8th Oct 2020, 12:10 AM
Solus
Solus - avatar
3 Answers
+ 3
Assignments are not copies. So the answer to the title is: NEVER. Case 1: x=s #now x refers to the string "string". This is not a copy, just two names that refer to the same value. s = s+"s" #a new string is created and s holds the new value. x doesn't care. case 2: This is exactly the same as case 1. No copies involved. d=c #They both refer to the same object c = c + ["KKK"] #The original list remains unchanged and a brand new list is created. d remains untouched. case 3: b = a #Again, this is not a copy, just two variables pointing at the same value. a[0] = "zzz" #the list changes and you can verify it printing either a or b. They are two ways to name the same thing. To be continued
8th Oct 2020, 7:20 AM
Kevin ★
+ 2
Part 1 Assignment is different from creating shallow and deepcopy. When you perform assignment both objects refer to same location be it string or list ,when you add something new to a string you create a new string ,also string provides no method to mutate original one as they are immutable When you append something to a list you make changes to original list and any other list object pointing to that location also have updated values , a+=[1,2,3] makes changes to original list but a=a+[1,2,3] creates a new list object now coming to deepcopy and shallow copy, they create a new object always ,but both differ in the way they implement it, Here is an example a=[[1,2,3],[4,5,6]] b=a.copy.copy() //creates a shallow copy meaning the inner list objects still points to same location for both a and b but they themselves points to different location so for example doing something like this b.append([1,2,3]) print(a,b) will print [[1,2,3],[4,5,6]] for a and [[1,2,3],[4,5,6],[1,2,3]] for b
8th Oct 2020, 8:02 AM
Abhay
Abhay - avatar
+ 2
Part 2 But if we do something like the following , b[0].append(3) and print it you will see that a is [[1,2,3,3],[4,5,6]] and b is [[1,2,3,3],[4,5,6]] So you can clearly see how shallow copy works ,inner list objects still points to same location but in deep objects any changes made to a list doesn't affect other list whose deep copy we make
8th Oct 2020, 9:08 AM
Abhay
Abhay - avatar