My understanding is that
assignment : This is easy to say , A man named Zhangsan ,A Call him Zhang San ,B Call him Zhang Si , But it's the same person , Zhang Si is just a nickname for him , If Zhang San is beaten , That is, Zhang Si was beaten
Deep copy : It's easy to understand , Cloned an identical person , Zhang San , Have a son Zhang Er , We do a deep copy , Cloned a Zhang 3 2 , A Zhang No. 2 , No. 2 Zhang is the son of No. 2 Zhang , Zhang San's leg was broken , No. 3 and No. 2 have not changed , Zhang Er's leg was broken , Zhang Er and No. 2 have not changed , A fully autonomous father and son
Shallow copy : A state between replication and deep copy , For example, Zhang San has a son, Zhang Er , I cloned Zhang San into a Zhang San No. 2 , But we have a common son, Zhang Er , If Zhang San's son had his leg broken , It means that the son of Zhang No. 3 and No. 2 has broken his leg , But if Zhang San has hemorrhoids or his leg is broken , For the cloned Zhang 3 and 2 , It is unaffected , This is the time , Lao Tzu is autonomous , Sons are shared
""" @File : qianCopy.py @Contact : [email protected] @Modify Time @Author @Version ------------ ------- -------- 2022/6/16 8:47 Afternoon Zhang Yin 1.0 @Desciption: """
import copy
alist = [1, 2, 3, ["a", "b"]]
# Shallow copy
blist = copy.copy(alist)
# assignment
clist = alist
# Deep copy
dlist = copy.deepcopy(alist)
print(alist)
print(blist)
print(clist)
print(dlist)
alist.append(4)
print(alist)
print(" If it's a shallow copy , There's no object to copy ")
print(blist)
print(" assignment , Passing a reference to an object , How does the original data change ,clist How to change ")
print(clist)
print(" Deep copy , No matter how the original data changes ,dlist All the same ")
print(dlist)
alist[3].append("cccc")
print(alist)
print(" If it's a shallow copy , There's no object to copy , So the original data alist After the child object pair of is changed , Shallow copy of child objects blist The child objects of will also change ")
print(blist)
print(" assignment , Passing a reference to an object , How does the original data change ,clist How to change ")
print(clist)
print(" Deep copy , No matter how the original data changes ,dlist All the same ")
print(dlist)
dlist.append(99)
print(alist)
print(blist)
print(clist)
print(" Deep copy , modify dlist It will not change the original data , It's independent of each other ")
print(dlist)
blist[3].append(101)
print(" Shallow copy , modify blist The children of , It will also affect the child objects of the original data ")
print(alist)
print(blist)
print(clist)
print(dlist)
result
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b'], 4]
If it's a shallow copy , There's no object to copy
[1, 2, 3, ['a', 'b']]
assignment , Passing a reference to an object , How does the original data change ,clist How to change
[1, 2, 3, ['a', 'b'], 4]
Deep copy , No matter how the original data changes ,dlist All the same
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b', 'cccc'], 4]
If it's a shallow copy , There's no object to copy , So the original data alist After the child object pair of is changed , Shallow copy of child objects blist The child objects of will also change
[1, 2, 3, ['a', 'b', 'cccc']]
assignment , Passing a reference to an object , How does the original data change ,clist How to change
[1, 2, 3, ['a', 'b', 'cccc'], 4]
Deep copy , No matter how the original data changes ,dlist All the same
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b', 'cccc'], 4]
[1, 2, 3, ['a', 'b', 'cccc']]
[1, 2, 3, ['a', 'b', 'cccc'], 4]
Deep copy , modify dlist It will not change the original data , It's independent of each other
[1, 2, 3, ['a', 'b'], 99]
Shallow copy , modify blist The children of , It will also affect the child objects of the original data
[1, 2, 3, ['a', 'b', 'cccc', 101], 4]
[1, 2, 3, ['a', 'b', 'cccc', 101]]
[1, 2, 3, ['a', 'b', 'cccc', 101], 4]
[1, 2, 3, ['a', 'b'], 99]