On 19Apr2015 15:09, Cameron Simpson <c...@zip.com.au> wrote:
On 18Apr2015 23:26, boB Stepp <robertvst...@gmail.com> wrote:
On Sat, Apr 18, 2015 at 11:08 PM, Cameron Simpson <c...@zip.com.au> wrote:
Sometimes you want a "deep" copy, where "b" would have got a copy of the
iriginal x-y list. See the "copy" module's "deepcopy" function, which
supplies this for when it is needed:

https://docs.python.org/3/library/copy.html#copy.deepcopy

In this reference, part of it states:

"Two problems often exist with deep copy operations that don’t exist
with shallow copy operations:

Recursive objects (compound objects that, directly or indirectly,
contain a reference to themselves) may cause a recursive loop.
Because deep copy copies everything it may copy too much, e.g.,
administrative data structures that should be shared even between
copies."

If I am understanding things correctly, should not that last sentence
read instead:

"...structures that should *not* be shared even between copies." ???

No, the text is correct.

Um, my explaination was incomplete.

The first sentence quoted explains why it is necessary for deepcopy keep track of copied objects in order to correctly duplicate objects just once instead of twice or more, let alone in an unbounded way if there is a loop (A->B->C->A). It does this with a "memo" dictionary of already copied objects so it can know them when it sees them again.

The second sentence "Because deep copy copies everything it may copy too much, e.g., administrative data structures that should be shared even between copies" is an issuewhich is addressed lower down when mentioning the __copy_ and __deepcopy__ methods. And it is not necessarily obvious.

Suppose you've got a data structure of objects, which should broadly be copied. However, _internally_, these objects may use some external facility. Further, suppose that facility can't be copied; perhaps it is a reference to a databse or something built around something like that. Like this:

 G -> A
   -> B
   -> _open_database

When you deepcopy that data sctructure you want to copy everything, but _not_ copy the external facility object. So for the above example, after the deepcopy you want this:

 Gcopy -> Acopy
       -> Bcopy
       -> _open_database

i.e. the same reference to the database, but copies of "A" and "B".

If you give "G"'s class a .__deepcopy__ method, then that will be called by deepcopy() to make a copy of "G" by calling "G.__deepcopy__(memodict)". And G's class will define its __deepcopy__ method to copy "A" and "B" but not "_open_database".

Most classes do not need this and deepcopy() just copies everything.

Does this clarify things?

Cheers,
Cameron Simpson <c...@zip.com.au>

No wonder these courtroom dramas are so popular. Half the population are
lawyers, judges, or some other kind of criminal.        - Jake Vest 1986
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to