# 等号赋值
# l0 = [1, 3, 5]
# l2 = [1, 3, 5]
# l0.append(7)
# print(l0, l2)
# print(l0 == l2, l0 is l2, id(l0), id(l2))
# 等号赋值: 直接赋予内存地址 两个变量最终还是同一个内存地址
# 修改一个另一个同步更改
# l0 = [1, 3, 5]
# l2 = l0
# l0.append(7)
# print(l0, l2)
# print(l0 is l2, id(l0), id(l2))
# 浅拷贝 把不可变类型的值拷贝 把可变类型地址拷贝
# l3 = [1, 3, 5, ["a", "b"]]
# l4 = l3.copy()
# l3.append(7)
# print(l3, l4, l3 is l4, id(l3), id(l4))
# l3[3].append("c")
# print(l3, l4, l3[3] is l4[3], id(l3[3]), id(l4[3]))
# import copy
# l3 = [1, 3, 5, ["a", "b"]]
# l4 = copy.copy(l3)
# print(l3 is l4)
# print(l3[3] is l4[3])
#
# l5 = [1, 3, 5, ["a", "b"]]
# l6 = copy.deepcopy(l5)
# print(l5 is l6, l5[3] is l6[3])
# l5.append(7)
# print(l5, l6)
# l5[3].append("c")
# print(l5, l6)
obj = {
"name": "马云",
"age": 18,
"address": ["杭州", "北京", ["北京南", "北京西"]],
"hobby": {
"1": "足球",
"2": "钓鱼"
}
}
import copy
obj4 = copy.copy(obj)
obj["address"][0] = "郑州"
obj["address"][2].append("北京北")
print(obj4)
# 等号 赋值 完全拷贝引用 相当于同一个
# obj2 = obj
# obj["age"] = 20
# obj["address"].append("上海")
# obj["hobby"]["3"] = "骑车"
# print(obj2)
# 浅拷贝: 半独立 不可变类型拷贝值 可变类型拷贝地址
# import copy
# # obj3 = obj.copy()
# obj3 = copy.copy(obj)
# print(obj is obj3)
# obj["age"] = 20
# obj["address"].append("上海")
# obj["hobby"]["3"] = "骑行"
# print(obj3)
# 深拷贝
# import copy
# obj4 = copy.deepcopy(obj)
# print(obj4 is obj, obj4["address"] is obj["address"], obj4["hobby"] is obj["hobby"])
# obj["age"] = 20
# obj["address"].append("上海")
# obj["hobby"]["3"] = "骑车"
# print(obj4)
标签:obj,python,深浅,l3,l0,print,拷贝,copy,append
From: https://blog.csdn.net/zhangzhaoyuxunlei/article/details/140444931