Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Shallow and Deep Copying in Python

Tech May 11 2

When working with mutable objects in Python—such as lists, dictionaries, or NumPy arrays—it's essential to distinguish between shallow and deep copying to prevent unintended side effects.

Consider the following assignment:

self.sd_mx = self.adj_mx.copy()

This uses the .copy() method to create a new object rather than assigning by reference.

Reference Assignment vs. Copying

  • Reference assignment:

    self.sd_mx = self.adj_mx
    

    Here, both variables point to the same underlying object. Any in-place modification to self.sd_mx will also alter self.adj_mx, because they share memory.

  • Shallow copy:

    self.sd_mx = self.adj_mx.copy()
    

    This creates a new container object, but its contents still reference the same elements as the original. For flat structures containing immutable types (e.g., integers in a NumPy array), this effectively isolates modifications.

Practical Example with NumPy

import numpy as np

class MatrixHandler:
    def __init__(self, base_matrix):
        self.original = base_matrix
        self.working_copy = self.original.copy()

    def alter_working(self):
        self.working_copy[0, 0] = -1

# Initialize with a 2x2 array
base = np.array([[5, 6], [7, 8]])
handler = MatrixHandler(base)

handler.alter_working()

print("Original matrix:")
print(handler.original)

print("Working copy:")
print(handler.working_copy)

Output:

Original matrix:
[[5 6]
 [7 8]]
Working copy:
[[-1  6]
 [ 7  8]]

The change to working_copy does not affect original, confirming that .copy() produced an independent array.

For nested structures (e.g., a list of lists), a shallow copy may stil share inner objects, necessitating a deep copy via copy.deepcopy(). However, for NumPy arrays—which are homogeneous and flat in memory—a shallow copy suffices to achieve full data independence.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.