Special Method Protocols
__deepcopy__() — copy protocol
Customize recursive copying. Pass the memo to recursive deepcopy calls; register the new object before following cyclic references. The memo is per copying pass, not a global cache.
Formal Syntax & Structure
python syntaxpython
def __deepcopy__(self, memo):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| memo | dict | Current copy memo. |
Return Value
object
Deeply copied object.
Working Example
Example Snippetpython
import copyclass Node: def __init__(self,items): self.items=items def __deepcopy__(self,memo): clone=Node([]) memo[id(self)]=clone clone.items=copy.deepcopy(self.items,memo) return clonedef demo(): n=Node([1]); clone=copy.deepcopy(n) return clone.items==n.items, clone.items is n.itemsprint(demo())