Understanding Variable Assignment and Deep vs. Shallow Copy in Python π
Understanding Variable Assignment and Deep vs. Shallow Copy in Python π
π Variable Assignment in Python
In Python, variables act as references to objects in memory. When you assign x = 3
, Python:
Creates the object
3
in memory.Associates the variable
x
with this memory address.
Example:
x = 3
y = "hello"
num = y # Assigns the value of y ("hello") to num
y = 5 # Overwrites y with 5
Behavior:
num
holds"hello"
even aftery
is reassigned, asnum
references the original value ofy
.Reassigning
y
does not affectnum
.
Quiz Recap:
x = 3
y = "hello"
num = y
y = 5
print(num) # Output: hello
print(y) # Output: 5
Key Takeaway: Variables only update their value when explicitly reassigned.
π Shallow Copy vs. Deep Copy
Python provides two main ways to copy objects: shallow copy and deep copy. These are critical when working with mutable data types like lists, dictionaries, and other nested objects.
1οΈβ£ Shallow Copy
A shallow copy creates a new object but copies references to the elements within the original object. Changes to the elements in the copied object affect the original if those elements are mutable.
Example:
original_list = [1, 2, 3]
shallow_copy = original_list[:]
shallow_copy[0] = 10
print(original_list) # Output: [1, 2, 3]
print(shallow_copy) # Output: [10, 2, 3]
Changing
shallow_copy
does not affectoriginal_list
because the top-level objects are independent.However, if the list contains nested mutable objects, those shared references will reflect changes.
Example with Nested Objects:
original_list = [[1, 2], [3, 4]]
shallow_copy = original_list[:]
shallow_copy[0][0] = 10
print(original_list) # Output: [[10, 2], [3, 4]]
print(shallow_copy) # Output: [[10, 2], [3, 4]]
Why? The top-level list is copied, but the nested lists inside are shared references.
2οΈβ£ Deep Copy
A deep copy creates a completely independent copy of the object and recursively copies all nested objects. Changes to the deep copy do not affect the original object.
Example:
import copy
original_list = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(original_list)
deep_copy[0][0] = 10
print(original_list) # Output: [[1, 2], [3, 4]]
print(deep_copy) # Output: [[10, 2], [3, 4]]
Why? Each nested list is duplicated, so modifications deep_copy
do not propagate to original_list
.
π§ Understanding Memory Allocation
Immutable Objects (e.g., integers, strings, tuples):
Copied by value.
Each variable assignment creates a new reference.
x = 10 y = x y = 20 print(x) # Output: 10
Mutable Objects (e.g., lists, dictionaries, sets):
Copied by reference in a shallow copy.
Deep copies break this reference by duplicating nested objects.
π οΈ Best Practices for Copying Objects
Use Shallow Copy:
When the object contains no nested mutable objects.
For performance efficiency when you donβt plan to modify the copyβs contents.
Use Deep Copy:
When working with complex nested data structures.
When you need a fully independent duplicate.
π Key Takeaways
Variable Assignments:
Assignments create references to objects.
Reassigning a variable does not affect other variables referencing the original value.
Shallow Copy:
Creates a new object but shares references for nested objects.
Changes to mutable nested objects affect the original.
Deep Copy:
Recursively duplicates all objects, ensuring complete independence.
Memory Allocation:
Immutable objects are copied by value.
Mutable objects are copied by reference unless explicitly deep copied.
π Quiz Time
Question:
What will the following code output?
import copy
original = [[1, 2], [3, 4]]
shallow = original[:]
deep = copy.deepcopy(original)
shallow[0][0] = 99
deep[1][0] = 88
print(original)
Answer:
[[99, 2], [3, 4]]
shallow[0][0] = 99
: Modifiesoriginal
because the nested list is shared.deep[1][0] = 88
: Does not modifyoriginal
because itβs a deep copy.
π Next Steps
In the next section, weβll delve into control structures like if-else
statements and loops, allowing you to write dynamic, interactive programs. Stay tuned! π
Last updated