What is the difference between shallow copy, deepcopy and normal assignment operation?

后端 未结 11 2219
温柔的废话
温柔的废话 2020-11-21 07:11
import copy

a = \"deepak\"
b = 1, 2, 3, 4
c = [1, 2, 3, 4]
d = {1: 10, 2: 20, 3: 30}

a1 = copy.copy(a)
b1 = copy.copy(b)
c1 = copy.copy(c)
d1 = copy.copy(d)


prin         


        
11条回答
  •  梦毁少年i
    2020-11-21 07:24

    Not sure if it mentioned above or not, but it's very importable to undestand that .copy() create reference to original object. If you change copied object - you change the original object. .deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.

    And yes, .deepcopy() copies original object recursively, while .copy() create a reference object to first-level data of original object.

    So the copying/referencing difference between .copy() and .deepcopy() is significant.

提交回复
热议问题