列表
# 浅拷贝 [:]
old_list= [1,2, [3,4]]
new_list= old_list[:]
old_list.append(5)
old_list[2][0]+= 97
print("Old list:", old_list,"old list id:",id(old_list)," old list[0] id:",id(old_list[2]))
print("new list:", new_list,"new list id:",id(new_list)," new list[0] id:",id(new_list[2]))
# 输出结果
Oldlist: [1,2, [100,4],5] oldlist id:4537660608 oldlist[0]id:4537659840
newlist: [1,2, [100,4]] newlist id:4537711424 newlist[0]id:4537659840
工厂函数简介
可变对象的工厂函数
列表
old_list= [1,2, [3,4]]
new_list= list(old_list)
old_list.append(5)
old_list[2][0]+= 97
print("Old list:", old_list,"old list id:",id(old_list)," old list[0] id:",id(old_list[2]))
print("new list:", new_list,"new list id:",id(new_list)," new list[0] id:",id(new_list[2]))
集合
old_set= {1,2,3}
new_set= set(old_set)
old_set.add(4)
print("Old set:", old_set,"old set id:",id(old_set))
print("new set:", new_set,"new set id:",id(new_set))
# 输出结果
Oldset: {1,2,3,4} oldset id:4484723648
newset: {1,2,3} newset id:4484723872
字典
old_dict= {"name":"小明"}
new_dict= dict(old_dict)
old_dict["second"]= "Python"
print("Old dict:", old_dict,"old dict id:",id(old_dict))
print("new dict:", new_dict,"new dict id:",id(new_dict))
# 输出结果
Olddict: {'name':'小明','second':'Python'} olddict id:4514161536
newdict: {'name':'小明'} newdict id:4515690304
列表
old_list= [1,2, [3,4]]
new_list= old_list.copy()
old_list.append(5)
old_list[2][0]+= 97
print("Old list:", old_list,"old list id:",id(old_list)," old list[0] id:",id(old_list[2]))
print("new list:", new_list,"new list id:",id(new_list)," new list[0] id:",id(new_list[2]))
# 输出结果
Oldlist: [1,2, [100,4],5] oldlist id:4309832000 oldlist[0]id:4310372992
newlist: [1,2, [100,4]] newlist id:4309735296 newlist[0]id:4310372992
集合
old_set= {1,2,3}
new_set= old_set.copy()
old_set.add(4)
print("Old set:", old_set,"old set id:",id(old_set))
print("new set:", new_set,"new set id:",id(new_set))
# 输出结果
Oldset: {1,2,3,4} oldset id:4309931392
newset: {1,2,3} newset id:4309930944
字典
old_dict= {"name":"小明"}
new_dict= old_dict.copy()
old_dict["second"]= "Python"
print("Old dict:", old_dict,"old dict id:",id(old_dict))
print("new dict:", new_dict,"new dict id:",id(new_dict))
# 输出结果
Olddict: {'name':'小明','second':'Python'} olddict id:4308452288
newdict: {'name':'小明'} newdict id:4308452224
源码
def copy(self,*args,**kwargs):# real signature unknown
""" Return a shallow copy of the list. """
pass
已经写的很清楚,这是浅拷贝
列表
from copyimport copy
old_list= [1,2, [3,4]]
new_list= copy(old_list)
old_list.append(5)
old_list[2][0]+= 97
print("Old list:", old_list,"old list id:",id(old_list)," old list[0] id:",id(old_list[2]))
print("new list:", new_list,"new list id:",id(new_list)," new list[0] id:",id(new_list[2]))
# 输出结果
Oldlist: [1,2, [100,4],5] oldlist id:4381013184 oldlist[0]id:4381159936
newlist: [1,2, [100,4]] newlist id:4381012800 newlist[0]id:4381159936
集合
from copyimport copy
old_set= {1,2,3}
new_set= copy(old_set)
old_set.add(4)
print("Old set:", old_set,"old set id:",id(old_set))
print("new set:", new_set,"new set id:",id(new_set))
# 输出结果
Oldset: {1,2,3,4} oldset id:4381115552
newset: {1,2,3} newset id:4381115776
字典
from copyimport copy
old_dict= {"name":"小明"}
new_dict= copy(old_dict)
old_dict["second"]= "Python"
print("Old dict:", old_dict,"old dict id:",id(old_dict))
print("new dict:", new_dict,"new dict id:",id(new_dict))
# 输出结果
Olddict: {'name':'小明','second':'Python'} olddict id:4381159680
newdict: {'name':'小明'} newdict id:4379632576