ππ Deep Copy vs. Shallow Copy in Python ππ§
When youβre duplicating data structures in Python, the depth of the copy matters. Let's dissect the intricacies of deep and shallow copies, and why they're crucial for advanced Python development.
π Understanding the core difference:
Shallow Copy: Only the outermost object is duplicated, while the inner objects are still references and not actual copies. The `copy` module's `copy()` function can achieve this.
Deep Copy: Both the outer and inner objects are duplicated, ensuring that no referenced objects from the source are left. This can be accomplished using the `copy` module's `deepcopy()` function.
π **Code Illustration**:
```python
# ππ Deep Copy vs. Shallow Copy in Pythone ππ§
import copy
# Shallow copy
original_list = [[1, 2, 3], [4, 5, 6]]
shallow_copied_list = copy.copy(original_list)
shallow_copied_list[0][0] = 999
# Deep copy
original_list_2 = [[1, 2, 3], [4, 5, 6]]
deep_copied_list = copy.deepcopy(original_list_2)
deep_copied_list[0][0] = 999
print(original_list) # [[999, 2, 3], [4, 5, 6]]
print(original_list_2) # [[1, 2, 3], [4, 5, 6]]
```
π‘ Here, modifying the shallow-copied list changed the original list as well. But the deep-copied list remained isolated.π‘
---------------
π Behind the curtains:
Python stores variables as references to objects. When doing a shallow copy, the outermost container gets a new reference, but the inner objects still point to the same references. In contrast, a deep copy ensures every object has a new reference, making the copied object totally independent.
-----------------------
π Real-life Use case code - Config File Generation:
Imagine a configuration template for a software package. You want to generate multiple configuration files from this template, with slight variations for different server environments, without altering the master template.
```python
# Original config template
config_template = {
"database": {
"user": "admin",
"password": "password123"
},
"server": {
"port": 8080
}
}
# Deep copy the template for production adjustments
production_config = copy.deepcopy(config_template)
production_config["database"]["password"] = "prod_secure_pass"
production_config["server"]["port"] = 8000
# The master template remains unchanged
print(config_template["database"]["password"]) # password123
print(config_template["server"]["port"]) # 8080
```
π By using a deep copy, modifications for the production environment don't affect the original template. This ensures consistent and error-free configuration generation.
---------------------
π If you enjoyed this post:
β
1. Checkout my recent Python book, covering 130 Python Core concepts like this across 350 pages and detail analysis.
πBook Link in my Twitter Bio
β
2. Give me a follow
@rohanpaul_ai for more of these every day and like and Retweet this tweet
------------
#llm #Largelanguagemodels #Llama2 #MachineLearning #ArtificialIntelligence #datascience #nlp #textprocessing #deeplearning #deeplearningai #100daysofmlcode #neuralnetworks #datascience #nlp #textprocessing #tensorflow #pytorch #deeplearning #deeplearningai #100daysofmlcode #neuralnetworks #pythonprogramming #python #100DaysOfMLCode #softwareengineer #dataanalysis #datascienceinterview #python #programming #coding #programmer #developer #coder #code #computerscience #pythonprogramming #software