深浅copy示例
赋值
#_*_ encoding: utf-8 _*_ @author: ty hery 2020/6/22
print('赋值')
will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = will
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------will--------------------')
#
print('id_wilber',id(wilber))
print('wilber',wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------wilber--------------------')
#
will[0] = "Wilber"
will[2].append("CSS")
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------修改后的will--------------------')
#
print('id_wilber',id(wilber))
print(wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------修改后的wilber--------------------')
输出:
赋值
id_will 167692616
will ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_will [37273304, 1701998912, 167690376]
-------------will--------------------
id_wilber 167692616
wilber ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_wilber [37273304, 1701998912, 167690376]
-------------wilber--------------------
id_will 167692616
will ['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
id_ele_will [167667000, 1701998912, 167690376]
-------------修改后的will--------------------
id_wilber 167692616
['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
id_ele_wilber [167667000, 1701998912, 167690376]
-------------修改后的wilber--------------------
浅拷贝, 切片也是浅拷贝
print('浅拷贝')
import copy
will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = copy.copy(will)
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------will--------------------')
print('id_wilber',id(wilber))
print('wilber',wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------wilber--------------------')
will[0] = "Wilber"
will[2].append("CSS")
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------修改后的will--------------------')
print('id_wilber',id(wilber))
print(wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------修改后的wilber--------------------')
输出:
浅拷贝
id_will 167692616
will ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_will [42450648, 1701998912, 167690376]
-------------will--------------------
id_wilber 167792904
wilber ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_wilber [42450648, 1701998912, 167690376]
-------------wilber--------------------
id_will 167692616
will ['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
id_ele_will [167667000, 1701998912, 167690376]
-------------修改后的will--------------------
id_wilber 167792904
['Will', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
id_ele_wilber [42450648, 1701998912, 167690376]
-------------修改后的wilber--------------------
深拷贝
import copy
will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = copy.deepcopy(will)
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------will--------------------')
print('id_wilber',id(wilber))
print('wilber',wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------wilber--------------------')
will[0] = "Wilber"
will[2].append("CSS")
print('id_will',id(will))
print('will',will)
print('id_ele_will',[id(ele) for ele in will])
print('-------------修改后的will--------------------')
print('id_wilber',id(wilber))
print(wilber)
print('id_ele_wilber',[id(ele) for ele in wilber])
print('-------------修改后的wilber--------------------')
输出:
id_will 167692616
will ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_will [42909400, 1701998912, 167690376]
-------------will--------------------
id_wilber 167792904
wilber ['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_wilber [42909400, 1701998912, 167792840]
-------------wilber--------------------
id_will 167692616
will ['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
id_ele_will [167667000, 1701998912, 167690376]
-------------修改后的will--------------------
id_wilber 167792904
['Will', 28, ['Python', 'C#', 'JavaScript']]
id_ele_wilber [42909400, 1701998912, 167792840]
-------------修改后的wilber--------------------
写入自己的博客中才能记得长久