Understanding Deep Copy and Shallow Copy in Python 3
This article explains the concepts, differences, and practical code examples of deep copy and shallow copy in Python 3, illustrating how each method works, their impact on nested objects, and how to apply them in API automation parameterization scenarios.
In Python programming, deep copy and shallow copy are common ways to duplicate objects. They differ in implementation, so it is important to understand their distinctions. This article details the concepts and differences of deep copy and shallow copy in Python 3, and provides example code relevant to API automation parameterization.
1. Deep Copy
A deep copy creates a new object that fully replicates the original object and all of its sub‑objects, resulting in an independent copy with no shared references.
The implementation recursively copies the entire object structure, including attributes and nested objects. If the original object contains references to other objects, deep copy recursively duplicates those referenced objects as well.
Deep copy can be performed using the copy.deepcopy() function or specific object methods such as list.copy() and dict.copy() .
Example code for deep copy:
import copy # Original object original_dict = {"name": "John", "age": 30} # Deep copy deep_copy_dict = copy.deepcopy(original_dict) # Modify original object original_dict["name"] = "Alice" # Output results print("Original Dict:", original_dict) # Output: {'name': 'Alice', 'age': 30} print("Deep Copy Dict:", deep_copy_dict) # Output: {'name': 'John', 'age': 30}2. Shallow Copy
A shallow copy creates a new object that copies the original object's contents, but only copies references to its sub‑objects. Thus, the new object shares sub‑object references with the original.
The implementation copies the original object's attributes, but for nested objects it copies only the references without recursively duplicating them.
Shallow copy can be performed using the copy.copy() function or specific object methods such as list.copy() and dict.copy() .
Example code for shallow copy:
import copy # Original object original_dict = {"name": "John", "age": 30} # Shallow copy shallow_copy_dict = copy.copy(original_dict) # Modify original object original_dict["name"] = "Alice" # Output results print("Original Dict:", original_dict) # Output: {'name': 'Alice', 'age': 30} print("Shallow Copy Dict:", shallow_copy_dict) # Output: {'name": 'Alice', 'age': 30}3. Differences Between Deep and Shallow Copy
Deep copy duplicates all sub‑objects, creating completely independent objects with no shared references. Shallow copy only copies references to sub‑objects, so the original and the copy share those nested objects.
Because deep copy creates independent copies, modifications to one do not affect the other. With shallow copy, changes to shared sub‑objects affect both the original and the copy.
In API automation, parameter objects often need to be duplicated for different test scenarios. Deep copy provides isolated copies, while shallow copy allows shared sub‑objects when appropriate.
Example demonstrating both copies with request parameters:
import copy # Original request parameters original_params = {"username": "user1", "password": "pass1"} # Shallow copy of parameters shallow_copy_params = copy.copy(original_params) # Deep copy of parameters deep_copy_params = copy.deepcopy(original_params) # Modify original parameters original_params["username"] = "user2" # Output results print("Original Params:", original_params) # Output: {'username': 'user2', 'password': 'pass1'} print("Shallow Copy Params:", shallow_copy_params) # Output: {'username': 'user1', 'password': 'pass1'} print("Deep Copy Params:", deep_copy_params) # Output: {'username': 'user1', 'password': 'pass1'}The example shows that after modifying the original parameters, the shallow‑copied dictionary reflects the change in shared sub‑objects, whereas the deep‑copied dictionary remains unchanged.
4. Summary
Deep copy and shallow copy are essential object duplication techniques in Python. Deep copy creates independent copies of all nested objects, while shallow copy copies only references to nested objects. Choose deep copy when you need completely separate objects, and shallow copy when shared sub‑objects are acceptable, especially in API automation contexts.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.