Deepcopy Memo: Preserving Shared Children and Self-References
2026-11-18

Deepcopy Memo: Preserving Shared Children and Self-References

Deepcopy Memo processing preserves relationships inside a copied object graph. Two references to one source child can become two references to one copied child, while a cycle can close on the copied object instead of recursing forever. The relevant distinction is object identity: equal-looking contents do not by themselves identify which references should share a target.

A graph can contain more than a simple tree

The Python copy documentation explains that recursive objects can create loops during deep copying and that deepcopy keeps a memo of objects already handled in the current copying pass. It also permits user-defined classes to customize which components are copied. The memo addresses relationships during that pass rather than serving as a permanent archive of every object ever copied.

Consider a hypothetical ordinary Python list called child containing one string. Another list, root, contains child twice. Root has two element positions, but both positions refer to the same child list. Drawing two boxes merely because there are two element positions would already misdescribe the source graph.

When this ordinary list graph is deep-copied in one call, the copied root has two references to the same copied child. That copied child is distinct from the original child. Internal sharing has been preserved even though the mutable child was copied away from the source graph.

Sourceandcopyroot eachhavetworeferences toonesharedchild; copiedchild isdistinctfromoriginal.
Original object-identity graph forordinaryPythonlists, not pandas tablecopy. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The memo recognizes a previously visited identity

The CPython 3.14.0 copy implementation looks up a source object's identity in the memo before copying a non-atomic object again. When the first reference to child is processed, a new child list is constructed and remembered. Processing the second reference finds that remembered result and reuses it.

This does not merge arbitrary equal children. If root instead contains two separate lists with equal string contents, they have different identities. They can therefore produce two distinct copied lists. Equality of values and sharing of one object are separate facts that a relationship diagram should keep distinct.

For the shared-child example, changing the copied child through the first copied position would also be visible through the second copied position, because those two positions still share one target. That observation would not mean that the copied child is secretly the original child. The source-versus-copy boundary and the sharing within each boundary answer different questions.

Registering an empty result lets a cycle close

Now consider a different ordinary list that contains a reference to itself. The list handler constructs an empty destination list and places it in the memo before recursively processing the source elements. When recursion encounters the source list again, the memo already points to the unfinished destination.

The destination can therefore receive a reference to itself. The final structure is a new self-referential list, rather than an endless stack of newly created lists. Registering only after every descendant had been copied would be too late for this cycle: processing the descendant would need the same source identity again before any result had been remembered.

A self-referentialsource list maps toanewself-referentialcopy, eachwithonefinitereturningedge.
Conceptual deepcopy_list ordering; no infinite recursive-unfolding illustration. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The copying pass defines the sharing scope

A fresh ordinary call creates its own memo when none is supplied. Copying the same child in two separate calls does not promise to reuse the first call's destination. The two operations are different passes, so their separate results should not be interpreted as evidence that sharing was lost within one pass.

For a custom deepcopy method that needs to copy components, the documentation says to pass the same memo to recursive deepcopy calls and to treat it as opaque. Replacing that memo independently for every nested component can defeat the shared pass. The implementation's identity lookup explains the mechanism, while the public customization contract explains how participating code remains in that pass.

Preserved graph structure does not mean every node is new

The graph view also explains why a recursive structure should not be drawn as an endlessly expanding tree. In the self-reference example there is one list node with an edge back to that same node. Its copy likewise contains one new list node and a returning edge. Repeating the box at every apparent nesting level would turn one finite cyclic relationship into an infinite drawing and conceal the exact identity that the memo is reusing.

The implementation returns some atomic objects unchanged, and the documentation notes that functions and classes are returned as the original objects. User-defined copy behavior can also choose which components to share. Consequently, counting all equal identities across the source and result is not a universal test that every deep copy succeeded or failed.

The examples here deliberately use ordinary mutable lists to make the memo's role precise. They do not assert behavior for a third-party table container, a device handle or a saved source file. A file backup is another operation entirely. More object-model explanations appear in the technical reference collection and the engineering article directory.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我