[Tutor] lists, name semantics
boB Stepp
robertvstepp at gmail.com
Sun Apr 19 20:36:08 CEST 2015
On Sun, Apr 19, 2015 at 12:24 AM, Cameron Simpson <cs at zip.com.au> wrote:
> On 19Apr2015 15:09, Cameron Simpson <cs at zip.com.au> wrote:
>>
>> On 18Apr2015 23:26, boB Stepp <robertvstepp at gmail.com> wrote:
>>>
>>> On Sat, Apr 18, 2015 at 11:08 PM, Cameron Simpson <cs at zip.com.au> wrote:
[...]
>>> "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 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".
Ah! This example clarifies things for me. This provides a specific
context for the phrase, "...that should be shared even between
copies..." That now makes sense.
> 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?
Very much! Thanks!
--
boB
More information about the Tutor
mailing list