You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63-1Lines changed: 63 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6375,11 +6375,12 @@ Converts the integer Unicode code point into the corresponding Unicode string.
6375
6375
6376
6376
Apart from built-in functions, the **Python Standard Library** also contains a wide range of built-in modules which are a group of functions organized based on functionality.
6377
6377
6378
-
Some of the commonly used modules are mentioned below:
6378
+
Some commonly used modules are:
6379
6379
6380
6380
-`math` - Mathematical functions
6381
6381
-`random` - Generate pseudo-random numbers
6382
6382
-`statistics` - Statistical functions
6383
+
-`copy` - Create shallow and deep copy of objects
6383
6384
6384
6385
**Accessing Modules**
6385
6386
@@ -6611,6 +6612,67 @@ If there are multiple modes with the same count, the first occurrence in the seq
6611
6612
'a'
6612
6613
```
6613
6614
6615
+
## copy Module
6616
+
6617
+
### Limitation of Shallow Copy
6618
+
6619
+
`copy()` method does not recurse to create copies of the child objects, so if the child objects are mutable (example nested list) any modification in the child object will get reflected in the both the parent objects.
6620
+
6621
+
```python
6622
+
>>> old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
6623
+
6624
+
# Copying old list into a new list
6625
+
>>> new_list = old_list.copy()
6626
+
6627
+
# Checking if both lists are pointing to the same object
6628
+
>>>id(new_list)==id(old_list)
6629
+
False
6630
+
6631
+
# Checking if items of both lists are pointing to the same objects
6632
+
>>> [id(new_list[idx])==id(old_list[idx]) for idx inrange(len(old_list))]
6633
+
[True, True, True]
6634
+
6635
+
# Modify new list
6636
+
>>> new_list[1][1] =0
6637
+
>>> new_list
6638
+
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
6639
+
>>> old_list
6640
+
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
6641
+
```
6642
+
6643
+
As we can see in the output, `new_list[1][1]` was modified which is reflected in both `new_list` and `old_list`.
6644
+
6645
+
The `copy` module provides the `deepcopy()` function which is helpful in mitigating this issue.
6646
+
6647
+
### Deep Copy - deepcopy(x[, memo])
6648
+
6649
+
Deep copy overcomes the shortcomings of `copy()` and recursively creates copies of the child objects found in the original list. This leads to the creation of an independent copy of the original.
6650
+
6651
+
```python
6652
+
>>>import copy
6653
+
>>> old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
6654
+
6655
+
# Copying old list into a new list
6656
+
>>> new_list = copy.deepcopy(old_list)
6657
+
6658
+
# Checking if both lists are pointing to the same object
6659
+
>>>id(new_list)==id(old_list)
6660
+
False
6661
+
6662
+
# Checking if items of both lists are pointing to the same objects
6663
+
>>> [id(new_list[idx])==id(old_list[idx]) for idx inrange(len(old_list))]
6664
+
[False, False, False]
6665
+
6666
+
# Modify new list
6667
+
>>> new_list[1][1] =0
6668
+
>>> new_list
6669
+
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
6670
+
>>> old_list
6671
+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
6672
+
```
6673
+
6674
+
Note the change in the `id()` equality of the children for `[True, True, True]` to `[False, False, False]`. As we can see in the output, `new_list[1][1]` was modified which gets reflected only in the `new_list`.
6675
+
6614
6676
# File Handling
6615
6677
6616
6678
## File Handling in Python - Introduction & Overview
0 commit comments