当你需要复制一个对象时,有时仅仅复制其引用是不够的,你需要创建一个新的独立的对象副本。这就是浅拷贝和深拷贝的用武之地。
summer camp浅拷贝 (Shallow Copy)
浅拷贝会创建一个新的对象,并尽可能地用原始对象中的元素填充新对象。对于不可变类型,这相当于复制了原来的值;而对于可变类型,则仅仅是复制了引用。
深拷贝 (Deep Copy)
深拷贝不仅会创建一个新的对象,还会递归地复制对象内部的所有对象。
summer camp示例代码
首先,我们需要导入 copy 模块,它提供了copy 和 deepcopy 函数。
1. 浅拷贝列表
importcopyoriginal_list = [1,2, [3,4]]shallow_copied_list =copy.copy(original_list)original_list.append(5)original_list[2].append(6)print("Original List:", original_list) # 输出: [1,2, [3,4,6],5]print("Shallow Copied List:", shallow_copied_list) # 输出: [1,2, [3,4,6]]
2. 深拷贝列表
importcopyoriginal_list = [1,2, [3,4]]deep_copied_list =copy.deepcopy(original_list)original_list.append(5)original_list[2].append(6)print("Original List:", original_list) # 输出: [1,2, [3,4,6],5]print("Deep Copied List:", deep_copied_list) # 输出: [1,2, [3,4]]
3. 浅拷贝字典
importcopyoriginal_dict = {'a':1,'b':2,'c': [3,4]}shallow_copied_dict =copy.copy(original_dict)original_dict['c'].append(5)print("Original Dict:", original_dict) # 输出: {'a':1,'b':2,'c': [3,4,5]}print("Shallow Copied Dict:", shallow_copied_dict) # 输出: {'a':1,'b':2,'c': [3,4,5]}
4. 深拷贝字典
importcopyoriginal_dict = {'a':1,'b':2,'c': [3,4]}deep_copied_dict =copy.deepcopy(original_dict)original_dict['c'].append(5)print("Original Dict:", original_dict) # 输出: {'a':1,'b':2,'c': [3,4,5]}print("Deep Copied Dict:", deep_copied_dict) # 输出: {'a':1,'b':2,'c': [3,4]}
5. 自定义类的浅拷贝
importcopyclassMyClass:def__init__(self, value):self.value = valueoriginal_obj = MyClass([1,2,3])shallow_copied_obj = copy.copy(original_obj)original_obj.value.append(4)print("Original Value:", original_obj.value)# 输出: [1, 2, 3, 4]print("Shallow Copied Value:", shallow_copied_obj.value)# 输出: [1, 2, 3, 4]
6. 自定义类的深拷贝
importcopyclassMyClass:def__init__(self, value):self.value = valueoriginal_obj = MyClass([1,2,3])deep_copied_obj = copy.deepcopy(original_obj)original_obj.value.append(4)print("Original Value:", original_obj.value)# 输出: [1, 2, 3, 4]print("Deep Copied Value:", deep_copied_obj.value)# 输出: [1, 2, 3]
7. 自定义类的 __copy__ 方法
import copyclassMyClass:def__init__(self, value):self.value = valuedef__copy__(self):returnMyClass(self.value[:])original_obj = MyClass([1,2,3])shallow_copied_obj = copy.copy(original_obj)original_obj.value.append(4)print("Original Value:", original_obj.value)# 输出: [1, 2, 3, 4]print("Shallow Copied Value:", shallow_copied_obj.value)# 输出: [1, 2, 3]
8. 自定义类的 __deepcopy__ 方法
import copyclassMyClass:def__init__(self, value):self.value = valuedef__deepcopy__(self, memo):returnMyClass(copy.deepcopy(self.value, memo))original_obj = MyClass([1,2,3])deep_copied_obj = copy.deepcopy(original_obj)original_obj.value.append(4)print("Original Value:", original_obj.value)# 输出: [1, 2, 3, 4]print("Deep Copied Value:", deep_copied_obj.value)# 输出: [1, 2, 3]
9. 复杂结构的浅拷贝
importcopyoriginal_complex = [1,2, [3,4], {'a':5,'b': [6,7]}]shallow_copied_complex =copy.copy(original_complex)original_complex[2].append(8)original_complex[3]['b'].append(9)print("Original Complex:", original_complex) # 输出: [1,2, [3,4,8], {'a':5,'b': [6,7,9]}]print("Shallow Copied Complex:", shallow_copied_complex) # 输出: [1,2, [3,4,8], {'a':5,'b': [6,7,9]}]
10. 复杂结构的深拷贝
importcopyoriginal_complex = [1,2, [3,4], {'a':5,'b': [6,7]}]deep_copied_complex =copy.deepcopy(original_complex)original_complex[2].append(8)original_complex[3]['b'].append(9)print("Original Complex:", original_complex) # 输出: [1,2, [3,4,8], {'a':5,'b': [6,7,9]}]print("Deep Copied Complex:", deep_copied_complex) # 输出: [1,2, [3,4], {'a':5,'b': [6,7]}]summer camp
通过这些示例,你可以看到浅拷贝和深拷贝之间的区别。浅拷贝只复制对象的第一层,而深拷贝则会递归复制所有层级的对
本文采摘于网络,不代表本站立场,转载联系作者并注明出处:https://www.iotsj.com//kuaixun/3615.html