I just came across what I find very strange behaviour that I would like to understand. Unfortunately I don't really know how to search for it.
If I have a function
def func(A, B):
A_ = A
A_[0, 1] = A_[0, 1] + 1
and a dictionary
dicto = {"A": np.zeros((3,3))} .
Now calling the function with func(dicto["A"])
will cause the array stored in the dictionary dicto to be changed. This is similar to a pointer, but I thought pointers don't exist in Python?
Why is that the case? I would expect A_
to be a local variable, meaning any change applied to it shouldn't affect the variable stored in the dictionary.
This behaviour changes to what I expected when the function is changed to
def func(A, B):
A_ = A * 1
A_[0, 1] = A_[0, 1] + 1 .
How can I achieve what I want without the extra calculation of A * 1
?