How to copy a dictionary and only edit the copy

前端 未结 20 2006
说谎
说谎 2020-11-21 06:59

Can someone please explain this to me? This doesn\'t make any sense to me.

I copy a dictionary into another and edit the second and both are changed. Why is this hap

20条回答
  •  悲哀的现实
    2020-11-21 07:56

    i ran into a peculiar behavior when trying to deep copy dictionary property of class w/o assigning it to variable

    new = copy.deepcopy(my_class.a) doesn't work i.e. modifying new modifies my_class.a

    but if you do old = my_class.a and then new = copy.deepcopy(old) it works perfectly i.e. modifying new does not affect my_class.a

    I am not sure why this happens, but hope it helps save some hours! :)

提交回复
热议问题