Difference [Python] equals assignment, copy, deepcopy of

Reference links:

1. Introduction of the python variable type immutable type: https://blog.csdn.net/answer3lin/article/details/86430074

(Reference may be reproduced blog immutable object type and the object type variable in Python )

2. Introduction difference equals assignment, copy, deepcopy of: https://blog.csdn.net/qq_26442553/article/details/82218403

Reader is advised to first understand the nature of the variables in python, the difference between variable and immutable object type object type, and then to copy depth will be easy to understand.


 1. equal sign "="

"=" Role is a reference to the original object address in memory, so that new objects point to this address.

1.1 immutable objects

For immutable objects whose value does not allow itself to be modified , modify the value of the variable to point to actually make a new object (the newly created object).

1. FIG immutable object type "=" Copy

1  # 1. = Copy immutable object value, and changes its value after the modified copy. 
2 val1 is = 1000
 . 3 val2 = val1 is
 . 4  Print ( " val1 is IS: {0}, val2 IS: {}. 1 " .format (val1 is, val2))
 . 5  # val1 is IS: 1000, val2 IS: 1000 
. 6  Print (ID ( val1), ID (val2))  
 . 7  # 139,692,380,387,760 139,692,380,387,760 
. 8  
. 9  # modify the value val1 because val1 is immutable type, the value is modified, re-allocates memory to a new value, and then he pointed. 
10 val1 is + =. 1
 . 11  Print (val1 is, ID (val1 is), val2, ID (val2)) 
 12 is  # 1001 1000 139,692,380,387,760 139,692,380,387,216

1.2 Variable Object Type

For variable object type "=" references the object address, modify, will change the contents of an object's value does not modify the object (does not create a new object, does not change the address pointer ).

Tips:

List address and the address of the object list [0] of the object is different. This list can be understood as a combination of a reference object which is inserted the address of each object.

Figure 2. list objects

1  # 1. = variable value replication object, and changes in its value after the modified copy. 
2 LS1 = [1,2,3,4 ]
 . 3 LS2 = LS1
 . 4  Print (ID (LS1), ID (LS2))
 . 5  # 140,520,429,283,400 140,520,429,283,400 
. 6  # directly = variable copy, the same memory address, the value of the same. 
. 7  Print (LS1, LS2) 
 . 8  # [. 1, 2,. 3,. 4] [. 1, 2,. 3,. 4] 
. 9  # directly = variable copy, the same memory address, the value of the same. 
10  # at this time to modify the value of a variable, because its value is variable, so only need to modify in the original memory address. 
. 11 ls1.append (. 5 )
 12 is  Print (ID (LS1), ID (LS2)) 
 13 is  # 140,520,429,283,400 140,520,429,283,400 
14 # Variable object to change the value, the same memory reference 
15  Print (LS1, LS2) 
 16  # [. 1, 2,. 3,. 4,. 5] [. 1, 2,. 3,. 4,. 5] because the two variables point to memory Like, the value is the same. 
. 17  
18 is  Print (ID (LS1), ID (LS2))
 . 19  Print (ID (LS1 [0]), ID (LS2 [0]))
 20 is  Print (ID (LS1 [. 1]), ID (LS2 [. 1 ] ))
 21 is  Print (ID (LS1 [2]), ID (LS2 [2 ]))
 22 is  # 140,520,429,283,400 140,520,429,283,400 
23 is  # 94,472,536,902,176 94,472,536,902,176 
24  # 94,472,536,902,208 94,472,536,902,208 
25  # 94,472,536,902,240 94,472,536,902,240

2. copy

2.1 immutable object type

Effects immutable objects with "=" in

2.2 Variable Object Type

For a copy of the object variable object type, will re-create a new object, if the original object is a composite object (such as List, etc. class instance), it will be a combination of the original object each object reference into the newly created object .

Note, copy only the first layer of the memory address of its constituent objects referenced over, it does not refer to address the composition of an object composed of object iteration! So, if the composition of the object is a variable type, although references to the same address, but its contents may vary. See the following two examples:

Example 1. 

 1 import copy
 2 ls1 =[1,2,3,4]
 3 ls2 = copy.copy(ls1)
 4 print(id(ls1), id(ls2))
 5 # 140616587448072 140616587497224
 6 
 7 ls1.append(5)
 8 print(ls1,ls2)  
 9 #[1, 2, 3, 4, 5] [1, 2, 3, 4]
10 
11 print(id(ls1), id(ls2))
12 print(id(ls1[0]), id(ls2[0]))
13 # 140616587448072 140616587497224
14 # 94889387870752 94889387870752

Example 2.

. 1 Origin = [. 1, 2, [. 3,. 4 ]]
 2 COPl = copy.copy (Origin)
 . 3 Origin [2] [0] = " Hey! "   # Modify the data value of the source 
. 4  Print (COPl) 
 . 5  # [ 1, 2, [ 'hey! ', 4]]

For the explanation of Example 2

  

FIG implemented 3. origin = [1, 2, [3, 4]] 4. copy of an internal configuration of FIG.

It can be seen copy only the address reference of the first layer is inserted in the new object . Only orange frame wherein the object is a new object.

So if you want a copy of the front and rear two objects completely independent of each other, independently of each other how to do it? With deepcopy, recursively composition inside the object underlying object reference.

3. deepcopy

copy and deepcopy difference:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

  • deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

1 origin = [1, 2, [3, 4]]
2 cop2 = copy.deepcopy(origin)
3 origin[2][0] = "hey!"  #修改数据源的值
4 print(cop2) # [1, 2, [3, 4]]

对比2.2的例2会发现deepcopy递归的将组合对象的每一层的对象都进行了复制。因此,original的对象与deep copy之后的对象是内存地址完全不同的,完全独立的。

         

图3. origin = [1, 2, [3, 4]]的内部结构                             图4. copy的实现                                                                              图5. deepcopy与copy实现的对比

其中橘色是deepcopy实现,不仅新建了第一层addr9处的对象,也递归地新建了addr10处的对象,并将addr10引用插入到addr9处的对象中;递归到指向不可变类型的对象为止;因而原对象与新对象是完全独立,互不影响的。

其中绿色是copy实现,仅新建了第一层addr8处的对象,直接将addr5处对象的引用插入到addr8中,并没有为其重新开辟空间,新建对象,因而,原对象中更深层次中内容的变换,会直接影响新对象内容,二者并非完全独立。

所以,慎用copy~

 

Guess you like

Origin www.cnblogs.com/shiyublog/p/11078313.html