I have the following class that simply create a dictionary of objects for me. Then copy the created dictionary, main_dict, to a new dictionary second_dic.
import numpy as np
class MainClass:
def __init__(self):
self.main_dict = dict()
def add(self, k, val):
self.main_dict[k]=val
main_obj = MainClass()
for i in range(1,5):
main_obj.add(k=i, val=np.random.randint(1,10))
print(main_obj.main_dict)
{1: 1, 2: 2, 3: 7, 4: 7}
second_dic = main_obj.main_dict.copy()
print(second_dic)
{1: 1, 2: 2, 3: 7, 4: 7}
It seems Python doesn't support pointers like C++. So when I change a value in my second_dic, the changes won't reflect in my main_dict. I am wondering what are my options in order to have this happens.
second_dic[1]=1000
print(second_dic)
{1: 1000, 2: 2, 3: 7, 4: 7}
print(main_obj.main_dict)
{1: 1, 2: 2, 3: 7, 4: 7}