copy module python code example
Example 1: example of a deep copy in python
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
old_list[1][0] = 'BB'
print("Old list:", old_list)
print("New list:", new_list)
Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Example 2: python copy instance
import copy
class A(object):
def __init__(self, a)
self.a = a
a = A(38)
a2 = copy.deepcopy(a)
a3 = copy.copy(a)
Example 3: .copy python
new_list = list.copy()