copy --- 浅层 (shallow) 和深层 (deep) 复制操作

源代码: Lib/copy.py


Python 的赋值语句不复制对象,而是创建目标和对象的绑定关系。对于自身可变,或包含可变项的集合,有时要生成副本用于改变操作,而不必改变原始对象。本模块提供了通用的浅层复制和深层复制操作,(如下所述)。

接口摘要:

copy.copy(obj)

Return a shallow copy of obj.

copy.deepcopy(obj[, memo])

Return a deep copy of obj.

copy.replace(obj, /, **changes)

Creates a new object of the same type as obj, replacing fields with values from changes.

Added in version 3.13.

exception copy.Error

针对模块特定错误引发。

浅层与深层复制的区别仅与复合对象(即包含列表或类的实例等其他对象的对象)相关:

  • 浅层复制 构造一个新的复合对象,然后(在尽可能的范围内)将原始对象中找到的对象的 引用 插入其中。

  • 深层复制 构造一个新的复合对象,然后,递归地将在原始对象里找到的对象的 副本 插入其中。

深度复制操作通常存在两个问题, 而浅层复制操作并不存在这些问题:

  • 递归对象 (直接或间接包含对自身引用的复合对象) 可能会导致递归循环。

  • 由于深层复制会复制所有内容,因此可能会过多复制(例如本应该在副本之间共享的数据)。

deepcopy() 函数用以下方式避免了这些问题:

  • 保留在当前复制过程中已复制的对象的 "备忘录" (memo) 字典;以及

  • 允许用户定义的类重写复制操作或复制的组件集合。

此模块不会复制模块、方法、栈追踪、栈帧、文件、套接字、窗口以及任何相似的类型。 它会通过不加修改地返回原始对象来(浅层或深层地)“复制”函数和类;这与 pickle 模块处理这类问题的方式是兼容的。

制作字典的浅层复制可以使用 dict.copy() 方法,而制作列表的浅层复制可以通过赋值整个列表的切片完成,例如,copied_list = original_list[:]

类可以使用与控制序列化(pickling)操作相同的接口来控制复制操作,关于这些方法的描述信息请参考 pickle 模块。实际上,copy 模块使用的正是从 copyreg 模块中注册的 pickle 函数。

In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__().

object.__copy__(self)

Called to implement the shallow copy operation; no additional arguments are passed.

object.__deepcopy__(self, memo)

Called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the __deepcopy__ implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument. The memo dictionary should be treated as an opaque object.

Function copy.replace() is more limited than copy() and deepcopy(), and only supports named tuples created by namedtuple(), dataclasses, and other classes which define method __replace__().

object.__replace__(self, /, **changes)

This method should create a new object of the same type, replacing fields with values from changes.

参见

模块 pickle

讨论了支持对象状态检索和恢复的特殊方法。