Jp Calderone said:
= =
It sounds terribly logical. Copying objects is extremely difficult =
to do correctly. There is no way to do it generically and correctly. =
Make the copy too shallow, and you get unwanted shared state. Make the =
copy too deep and the new object is useless. How do you decide how deep =
is deep enough? You can't, at least not without intimate knowledge of =
the object you're dealing with.
Once you figure out exactly how deep you need to make your copy, you =
can tell copy.deepcopy() how to behave with the copy_reg module. Of =
course, since this knowledge is quite closely tied to VPython, it should =
really be *part* of the VPython package, so that changes to the =
internals of VPython can be made right alongside changes to the code =
which knows how to copy its objects.
Well a quick look at copy_reg and I backed away. Particularly since I am
accessing the internet from a dial-up at the moment and am not in a position
to do the research necessary to get a handle on it. The module docs are
much too sketchy here for someone starting with this from where I am
starting. And as you say, there looked like I might need to get at VPython
internals to do it correctly.
However I did realize, in reading the docs, that overriding __copy__ might
be all I needed to do. Really what I was looking for was a copy of the
original instance with a fresh VPython object on which to operate.
Conceptually I am doing a non-destructive transformation of the instance,
with the transformed object the same, exscept at a transformed location.
Something like this seemed to work:
def __copy__(self):
newcopy=copy.deepcopy(self)
newcopy.vpythonobject=<intialize new vpython object here>
return newcopy
But before getting too far into testing whether this accomplished the copy I
was looking for (perhaps the success was more apparent than real, and the
advice to resort to copy_reg is more unavoidable than I understand) I
realized that I had misconceived something more fundamental. That the
normal instance creation process involves registerting the new instance with
the app in various ways, and that a copy won't accomplish this.
That is until wrote the last paragraph, and realized that I might be able to
throw the registration routines into the __copy__ method.
Hmmm.
Let's try.
Art