Skip to content

Commit d3dc992

Browse files
committed
Add copy module
1 parent 2752111 commit d3dc992

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6375,11 +6375,12 @@ Converts the integer Unicode code point into the corresponding Unicode string.
63756375

63766376
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.
63776377

6378-
Some of the commonly used modules are mentioned below:
6378+
Some commonly used modules are:
63796379

63806380
- `math` - Mathematical functions
63816381
- `random` - Generate pseudo-random numbers
63826382
- `statistics` - Statistical functions
6383+
- `copy` - Create shallow and deep copy of objects
63836384

63846385
**Accessing Modules**
63856386

@@ -6611,6 +6612,67 @@ If there are multiple modes with the same count, the first occurrence in the seq
66116612
'a'
66126613
```
66136614

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 in range(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 in range(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+
66146676
# File Handling
66156677

66166678
## File Handling in Python - Introduction & Overview

0 commit comments

Comments
 (0)