COSC 1336 | PyThon

ACC

Shallow v. Deep Copies

Foreword

One of the more tortuous topics in a beginning programming language course is that of shallow v. deep copies. It really is not difficult but it does require a little time, thinking, and practice with the concepts. The time we devoted to understanding the variable memory management model of Python in previous chapters will help us comprehend the shallow and deep copy topics.

Shallow Copy

With a shallow copy, only 'top-level' contents of the source list are copied into the destination list. See the lines of code below. The top-level objects in source_list are '1st_item' and '3rd_item'. The 2nd-level item in source_list is actually another list "['inner_1', 'inner_2']". With a shallow copy, the contents of the '1st_item' and '3rd_item' objects are copied but the address of the embedded list is copied. Repeat the previous sentence to yourself 5,000 times and you will have it.

 

The result of this is that, after the copy, if top_level objects are changed in either the source or destination list, the other list will not be changed. However, if an element of the embedded list is changed in either the source list or the destination list, the embedded list in the other list will also change. This happens because the embedded list addresses are the same.

There is one more thing to consider. If either of the lists (source or destination) changes the embedded list itself (and not just an element within the embedded list) then a new object is created for that embedded list. So, what do you think will happen? Since an entirely new object was created for one of the embedded lists, they no longer share the common address of the original object in the list.

 

Deep Copy

You can probably anticipate the behavior of deep copy operation after understanding the shallow copy. The deep copy copies all content and not the addresses. After a deep copy, there is no association between the source and destination lists. Therefore, if either list or the embedded lists are changed, the change will have no effect on the other list (or the embedded lists).

By the way, deepcopy() is not part of the 'core language'. Therefore, before we can use it, we must include the function from the library file which is accomplished on line 74:

from copy import deepcopy

Understanding of the shallow and deep copy concepts will come with study and practice.

Assignment Copy

For completeness, there is a section of the code example that covers the use of the assignment statement. We have seen in previous examples that when assigning one object to another the address that the source object contains is copied into the contents of the destination object such that both objects now point to the same address. The assignment copy can be seen as the opposite of the deep copy. Deep copy copies only content and assignment copy copies only addresses.

Code Example

Study and recode the example below. See the comments for more explanations of the operations.

 

Here's the output.

 

If, after coding the example and extensively studying the content, the shallow/deep topic is still confusing for you, it may be helpful to review the material at shallow v. deep copies for an alternative view of the topic.