shallow copy = copy @ level 1 only; any changes made at level 2,3,etc in either the original or the copy will reflect in the other. deep copy = copy @ all levels; a true independent copy ..you covered all possible scenarios of how a shallow copy can be made; also you covered how it will work in case of custom objects. Thanks a lot for clearing this concept for me. Subscribed!
This video is definitely underrated, I wish I could find the one guy who disliked this ! I got into so many problems in my first python project because I did not know these. Took me 5 hours to find where the problem was and I had to create my own copier functions(which was basically creating new object with same attributes of original objects, in some cases re assigning 15 attribute). I think python tutorials should mention this problem (some of them do) emphasis on its importance and give a practical complete solution for it. I have watched many tutorials and none of them provides a complete and clear and easy solution like you did. Thank you so much
Great video. Can you explain what kind of structure is considered 'deep'? From the example in your video, I could tell a list of lists is deep, since it is a nested structure of the same type. But if I have a list of dictionaries, is that considered deep or shallow assuming the values of each dict are plain?
in the person example when you do a shallow copy with copy.copy(p1) why the age of p1 is not affected? I thought shallow copy copies only the references
Shallow copys are 1 level deep. Modifying on level 1 does not affect the other list. Only if you use the copy asignment operator p1 = p2 then you copy only the references
Hi thank you for your video. It taught me a lot. But I have one question regarding the example at 6:58. I don't understand why the shallow copy can prevent the age of the original from being changed to 28? Thank you!
At 9:36, the change was done at the second level under the level of shallow copy. If changes at the first level under shallow copy won't affect original, as is the case in the person example at 6:58, why would changes two levels below the copy affect the original?
level 1 won't affect a shallow copy, but anything from level 2 and lower does affect it. This is because lower levels are not actually copied, you only copy the reference then.
shallow copy = copy @ level 1 only; any changes made at level 2,3,etc in either the original or the copy will reflect in the other.
deep copy = copy @ all levels; a true independent copy
..you covered all possible scenarios of how a shallow copy can be made; also you covered how it will work in case of custom objects. Thanks a lot for clearing this concept for me. Subscribed!
Glad it was helpful and thank you so much!
Fixes the frustration of so many unnecessary bugs I encoutnered while managing lists thank you!
Glad to help!
For many years I understood shallow and deep copy concept very wrongly. Damn!
Thank you for this video!
This video is definitely underrated, I wish I could find the one guy who disliked this ! I got into so many problems in my first python project because I did not know these. Took me 5 hours to find where the problem was and I had to create my own copier functions(which was basically creating new object with same attributes of original objects, in some cases re assigning 15 attribute). I think python tutorials should mention this problem (some of them do) emphasis on its importance and give a practical complete solution for it. I have watched many tutorials and none of them provides a complete and clear and easy solution like you did. Thank you so much
Good stuff dude. Straight and to the point.
A very clear explanation. Thank you
Excellent explanations, thanks
Glad it was helpful!
thanks! I assume then it's always safer to implement deep copy than shallow copy?
Great video. Can you explain what kind of structure is considered 'deep'? From the example in your video, I could tell a list of lists is deep, since it is a nested structure of the same type. But if I have a list of dictionaries, is that considered deep or shallow assuming the values of each dict are plain?
thanks,... nice video + helpful
Glad you like it :)
in the person example when you do a shallow copy with copy.copy(p1) why the age of p1 is not affected? I thought shallow copy copies only the references
Shallow copys are 1 level deep. Modifying on level 1 does not affect the other list. Only if you use the copy asignment operator p1 = p2 then you copy only the references
Great!
Very nice
2:26
import copy
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Company:
def __init__(self, boss, employee):
self.boss = boss
self.employee = employee
p1 = Person("Alex", 55)
p2 = Person("Joe", 18)
comp = Company(p1, p2)
company = copy.deepcopy(comp)
company.boss.age = 56
company.employee.name = "Mark"
print(company.boss.age)
print(company.employee.name)
good
Hi thank you for your video. It taught me a lot. But I have one question regarding the example at 6:58. I don't understand why the shallow copy can prevent the age of the original from being changed to 28? Thank you!
At 9:36, the change was done at the second level under the level of shallow copy. If changes at the first level under shallow copy won't affect original, as is the case in the person example at 6:58, why would changes two levels below the copy affect the original?
level 1 won't affect a shallow copy, but anything from level 2 and lower does affect it. This is because lower levels are not actually copied, you only copy the reference then.
Great!