python, [:] sense (deep copy and shallow copy)

python inside pass an object reference is equivalent to transfer address

id () function returns the object at the memory address

a = test showed a shallow copy, a and a test point to the same memory address or

b = test [:] is a deep copy, the equivalent of a copy of the memory address, again with this new variable b to point to the memory address

One problem is shallow copy will happen

Shallow Copy: Copy before, after a copy with the same memory address, once the value is modified, all variants will follow 

By a modification of the value, test also become. Can copy.copy or copy.deepcopy.

Both the efficiency is not the same, which is recursive, the speed slower than copy.copy

Immutable objects: int str float tuple, mutable objects: dict set list

Immutable objects: the value of the object is stored in the address will not be changed (so-called change is the creation of a new address and the value of the new object in the new address in the original object has not changed); mutable objects : objects stored in the address value will change in situ

Immutable objects str

Variable object list, change the value in place, the same memory address

 

 

 

 

Published 44 original articles · won praise 0 · Views 1902

Guess you like

Origin blog.csdn.net/weixin_39331401/article/details/104616759