copy --- 淺層 (shallow) 和深層 (deep) 複製操作

原始碼:Lib/copy.py


Python 的賦值陳述式不複製物件,而是建立目標和物件的繫結 (binding) 關係。對於可變 (mutable) 或包含可變項目 (mutable item) 的集合,有時會需要一份副本來改變特定副本,而不必改變其他副本。本模組提供了通用的淺層複製和深層複製操作(如下所述)。

介面摘要:

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

引發針對特定模組的錯誤。

淺層與深層複製的區別僅與複合物件(即包含 list 或類別的實例等其他物件的物件)相關:

  • 淺層複製建構一個新的複合物件,然後(在儘可能的範圍內)將原始物件中找到的物件的參照插入其中。

  • 深層複製建構一個新的複合物件,然後遞迴地將在原始物件裡找到的物件的副本插入其中。

深層複製操作通常存在兩個問題,而淺層複製操作並不存在這些問題:

  • 遞迴物件(直接或間接包含對自身參照的複合物件)可能會導致遞迴迴圈。

  • 由於深層複製會複製所有內容,因此可能會有過多複製(例如應該在副本之間共享的資料)。

deepcopy() 函式用以下方式避免了這些問題:

  • 保留在當前複製過程中已複製的物件的 memo 字典;以及

  • 允許使用者定義的類別複寫 (override) 複製操作或複製的元件集合。

該模組不複製模組、方法、堆疊追蹤(stack trace)、堆疊框(stack frame)、檔案、socket、視窗、陣列以及任何類似的型別。它透過不變更原始物件並將其回傳來(淺層或深層地)"複製"函式和類別;這與 pickle 模組處理這類問題的方式是相似的。

字典的淺層複製可以使用 dict.copy(),而 list 的淺層複製可以透過賦值整個 list 的切片 (slice) 完成,例如,copied_list = original_list[:]

類別可以使用與操作 pickle 相同的介面來控制複製操作,關於這些方法的描述資訊請參考 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 模組

支援物件之狀態檢索 (state retrieval) 和恢復 (restoration) 相關特殊方法的討論。