Deeper copy than deepcopy
cs at zip.com.au
Tue Oct 27 22:43:09 CET 2009
On 28Oct2009 08:23, I wrote:
| | I wanted to wind up with r being [[1, 2, 3], [1, 2, 999], [1, 2, 3]].
| | What's the right way to construct r as a list of *independent* d lists?
| Well, you would need to write your own. But consider this:
| x = [1, 2]
| Your deepercopy() function will explode.
I've thought of a comprimise that would work automatically: deepcopy
tracks all the objects in the structure and replicates their multiple
uses. For your purposes, you could write a version of deepcopy that did
a depth-first recursion and only tracked the ancestor objects.
That way the above x.append(x) recursive list would not duplicate the
inner "x", but an "x" in two subbranches of a structure _would_ be
replicated i.e. this:
x = [1,2]
y = [x, x]
would get two independent "x"s.
However, it would still mean a recursive structure would get non-independent
"x"s. Which is necessary to avoid an infinite copy but breaks your
Cameron Simpson <cs at zip.com.au> DoD#743
Every technical corrigendum is met by an equally troublesome new defect report.
- Norman Diamond <diamond at jit.dec.com>
More information about the Python-list