Skip to content

Deep vs Shallow Copy in Python: Understanding the Difference

[

Shallow vs Deep Copying of Python Objects

In Python, when working with mutable objects or collections, it is important to understand the difference between shallow copying and deep copying. Shallow copying creates a new collection object and populates it with references to the child objects found in the original, while deep copying recursively creates a fully independent clone of the original object and all of its children.

Making Shallow Copies

To create a shallow copy of a mutable object like a list, dictionary, or set, you can use the corresponding factory function and pass the original object as an argument. Here are some examples:

xs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ys = list(xs) # Shallow copy of xs

In this example, ys will be a new object with the same contents as xs, but modifying xs will not affect ys. However, since it is a shallow copy, ys still contains references to the original child objects:

xs.append(['new sublist'])
print(xs) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], ['new sublist']]
print(ys) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Making Deep Copies

If you want to create a fully independent clone of an object and all its children, you need to use deep copying. Python provides the copy module for this purpose. Here’s an example:

import copy
xs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ys = copy.deepcopy(xs) # Deep copy of xs

With a deep copy, modifying the original object will not affect the copy:

xs.append(['new sublist'])
print(xs) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], ['new sublist']]
print(ys) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Copying Arbitrary Python Objects

Shallow and deep copying can also be applied to custom objects by implementing the __copy__() and __deepcopy__() methods respectively. These methods should return a new object with the same state as the original.

import copy
class MyClass:
def __init__(self, data):
self.data = data
def __copy__(self):
return MyClass(self.data)
def __deepcopy__(self, memo):
return MyClass(copy.deepcopy(self.data, memo))

With these methods implemented, you can now use the copy module to make shallow or deep copies of instances of MyClass.

3 Things to Remember

When working with mutable objects or collections in Python:

  1. Assignment statements do not create copies of objects, only names bound to objects.
  2. Shallow copying creates a new collection object, but the child objects are still references to the original.
  3. Deep copying creates a fully independent clone of the original object and its children.

Understanding these concepts of shallow and deep copying is essential for manipulating objects in Python effectively.