python直接赋值、浅拷贝和深拷贝

时间:2022-08-08 19:48:33

 

# 解:
# import copy
# names1=['Amir','Barry','Cgakes','Dao',[11,22,33]]
# names2=names1#直接赋值,指向同一个对象
# names3=names1[:]#切片生成一个新的对象
# names4=copy.copy(names1)#浅拷贝,拷贝父对象,和切片效果一样
# names5=copy.deepcopy(names1)#深拷贝,拷贝父对象和子对象
# names1[0]='fuck'
# names1[-1][0]=44
# print(names1)
# print(names2)
# print(names3)
# print(names4)
# print(names5)

"""

['fuck', 'Barry', 'Cgakes', 'Dao', [44, 22, 33]]
['fuck', 'Barry', 'Cgakes', 'Dao', [44, 22, 33]]#父对象和子对象都是指向同一个内存地址
['Amir', 'Barry', 'Cgakes', 'Dao', [44, 22, 33]]#父对象是独立的,子对象还是指向同一个内存地址
['Amir', 'Barry', 'Cgakes', 'Dao', [44, 22, 33]]#父对象是独立的,子对象还是指向同一个内存地址
['Amir', 'Barry', 'Cgakes', 'Dao', [11, 22, 33]]#父对象和子对象都是独立的

 
 

"""