前言
在编写接口自动化测试脚本时,我们经常需要复制数据结构来模拟不同的测试场景。Python 提供了两种拷贝机制:浅拷贝和深拷贝。理解这两种拷贝的区别对保证测试数据的一致性和准确性至关重要。
浅拷贝 vs 深拷贝
浅拷贝:只复制引用,对于可变对象内部的元素不进行复制。
深拷贝:不仅复制引用,还会复制对象内部的所有元素。
拷贝工具
copy 模块:提供了 copy() 和 deepcopy() 函数。
内置函数:如 list.copy() 或 dict.copy()。
示例代码
示例 1: 基础列表拷贝
import copy
original = [1, 2, 3]
shallow_copy = original[:]
deep_copy = copy.deepcopy(original)
original.append(4)
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: [1, 2, 3, 4]
Shallow copy: [1, 2, 3]
Deep copy: [1, 2, 3]
示例 2: 列表嵌套列表
original = [[1, 2], [3, 4]]
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original[0].append(5)
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: [[1, 2, 5], [3, 4]]
Shallow copy: [[1, 2, 5], [3, 4]]
Deep copy: [[1, 2], [3, 4]]
示例 3: 列表切片拷贝
original = [1, 2, 3, [4, 5]]
shallow_copy = original[:]
original[3].append(6)
print("Original:", original)
print("Shallow copy:", shallow_copy)
输出结果:
Original: [1, 2, 3, [4, 5, 6]]
Shallow copy: [1, 2, 3, [4, 5, 6]]
示例 4: 字典拷贝
original = {'a': 1, 'b': 2}
shallow_copy = original.copy()
deep_copy = copy.deepcopy(original)
original['c'] = 3
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: {'a': 1, 'b': 2, 'c': 3}
Shallow copy: {'a': 1, 'b': 2}
Deep copy: {'a': 1, 'b': 2}
示例 5: 字典嵌套字典
original = {'a': 1, 'b': {'c': 2}}
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original['b']['c'] = 3
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: {'a': 1, 'b': {'c': 3}}
Shallow copy: {'a': 1, 'b': {'c': 3}}
Deep copy: {'a': 1, 'b': {'c': 2}}
示例 6: 自定义对象拷贝
class MyClass:
def __init__(self, x):
self.x = x
original = MyClass([1, 2])
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original.x.append(3)
print("Original:", original.x)
print("Shallow copy:", shallow_copy.x)
print("Deep copy:", deep_copy.x)
输出结果:
Original: [1, 2, 3]
Shallow copy: [1, 2, 3]
Deep copy: [1, 2]
示例 7: 元组拷贝
original = (1, 2, [3, 4])
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original[2].append(5)
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: (1, 2, [3, 4, 5])
Shallow copy: (1, 2, [3, 4, 5])
Deep copy: (1, 2, [3, 4])
示例 8: 集合拷贝
original = {1, 2, 3}
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original.add(4)
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: {1, 2, 3, 4}
Shallow copy: {1, 2, 3, 4}
Deep copy: {1, 2, 3}
示例 9: 多层次嵌套
original = [1, {'a': 2, 'b': [3, 4]}, 5]
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
original[1]['b'].append(5)
print("Original:", original)
print("Shallow copy:", shallow_copy)
print("Deep copy:", deep_copy)
输出结果:
Original: [1, {'a': 2, 'b': [3, 4, 5]}, 5]
Shallow copy: [1, {'a': 2, 'b': [3, 4, 5]}, 5]
Deep copy: [1, {'a': 2, 'b': [3, 4]}, 5]
示例 10: 自定义拷贝方法
class MyClass:
def __init__(self, x):
self.x = x
def custom_copy(self):
return MyClass(self.x[:])
original = MyClass([1, 2])
shallow_copy = original.custom_copy()
original.x.append(3)
print("Original:", original.x)
print("Shallow copy:", shallow_copy.x)
输出结果:
Original: [1, 2, 3]
Shallow copy: [1, 2]
总结
通过这些例子可以看出,浅拷贝仅复制顶层对象,而深拷贝会递归复制所有对象。在接口自动化测试中,选择合适的拷贝方式可以帮助我们构建出更加可靠和可维护的测试用例。
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走!
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。