Deepcopies of trees

Andrew Cooke andrew at
Fri May 5 15:03:54 CEST 2000


When I do a deepcopy of a structure without circular links, are all
objects inside dictionaries passed to __setstate__ fully instantiated?
If this is true of the code (which I can look at myself, I know), is
it also a design decision (ie will it always be so)?  Alternatively,
can anyone suggest appropriate solutions for the following problem:

I have a tree in which each node has a link to its parent.  This is
not absolutely necessary, but makes other code simpler.  I need to
copy a subtree from one part of the tree to another.  This must be a
deep copy as the tree will later be manipulated.  However, of course,
the deep copy must not copy parent objects (actually, only the parent
of the top node of the subtree, but it seems simplest to keep the
solution general).

So I am thinking of writing a __getstate__ that removes the parent
link from the dictionary and a __setstate__ that sets the parent of
subnodes as "self".  This last step requires that the subnodes exist -
hence my question.

I wouldn't even ask this, except that deepcopy *can* copy circular
links, so it *must* (I think!) in *at least that case* call setstate
with dictionaries that point to objects that do not "fully" exist (ie
haven't had setstate called)

On the other hand, I may be horribly confused ;-)


Sent via
Before you buy.

More information about the Python-list mailing list