copy.deepcopy question

Terry Reedy tjreedy at udel.edu
Tue Nov 12 10:00:34 EST 2002


"Markus von Ehr" <markus.vonehr at ipm.fhg.de> wrote in message
news:3DD0BFED.B59F49D9 at ipm.fhg.de...
> Hi,
>
> I have one object called Reference.
> Several Spectra objects include their own Reference object.
> To assign the Reference I want to pass the Reference object to
> several Spectra objects which have to make a copy to a member
variable.

The question is whether you really want to copy the object, so that
there are multiple objects with one reference to each, or just copy
the references so that there are multiple references to the single
object.  (Note that 'reference' here is the internal Python pointer to
objects, not to be confused with your Reference object.)

If you want each each Spectra to be able to independently modify its
own Reference, then you need copies of the object.

If Reference is only read and never changed, or if you want changes to
Reference made by one Spectra to be seen by all Spectras, then you
need only one object with multiple references.

> Every Spectra need its own copy because different Spectras may have
> different References.

I don't understand this at all.  If different Spectra have different
(in value) References, then there is no issue of copying.

> Is copy.deepcopy the right way to copy the Reference object to
> a Spectras member variable?

Possibly, but quite possibly not.  If you actually do want a copy, you
have to think about how deep the copying should go.

Terry J. Reedy





More information about the Python-list mailing list