To your Question 1):
Lets see what happens here first,
self.init_value = np.array([1,2])
a np.array object with the values provided is created in memory, to this the label self.init_value is assigned.
Now, at this step,
test = self.init_value
To the memory location pointed by self.init_value another label test is assigned. you can test this by modifying the func() as follows:
def func():
test = self.init_value
# Both these values will be same.
print(hex(id(test)))
print(hex(id(self.init_value)))
test[-1] = 0
return test
So when you do test[-1] = 0 you are basically accessing the last element of the object pointed by test and changing it to value 0, since both self.init_value and test point to the same object, it gets modified.
But if you reassign some other object with the same label name, the label gets removed from the previous object and gets attached to this new object. You can verify this by checking the memory location using the code:
hex(id(your_label))
To your Question 2:
If you don't want test to modify self.init_value, modify the code to use deepcopy() as follows:
import copy
test = copy.deepcopy(self.init_value)
int_valuedynamically. Usetest = self.init_value.copy()instead.