-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathCOPY.py
More file actions
49 lines (31 loc) · 863 Bytes
/
Copy pathCOPY.py
File metadata and controls
49 lines (31 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import copy
def example():
"""
It is not the copying example, b = a creates a new name in namespace which point to one object.
"""
a = [1, 2]
b = a
b[0] = "Both will be changed"
print(a, b)
def example1():
a = [[1,2,3]]
b = a[:]
print(id(a), id(b)) # Different ids
b[0][0] = 999
print(a, b) # But both will be changed, because [:] is shallow copy
def example1_with_deepcopy():
a = [[1,2,3]]
b = copy.deepcopy(a)
print(id(a), id(b)) # Different ids
b[0][0] = 999
print(a, b) # Only b will be changed
def example2():
old_list = [4, 5, 6, [2,3,3]]
new_list = copy.copy(old_list)
new_list[3] = [99999]
print(old_list, new_list) # Only new_list will be changed
if __name__ == '__main__':
example()
example1()
example1_with_deepcopy()
example2()