I'm kind of new to Python, and am having trouble understanding what "=" means. I will try and describe my current model of what it means, and then point out where it breaks.
My current model of "=" is that when we write A = B, we have A a reference, and B another reference (because "everything is a reference"). The operation "=" makes A point to the same object as B.
e.g
x = [1,2]
y = x
would have both x and y as references which point to the same list object. It (kind of) makes sense to me that
x[0] = 1
mutates x, as x is a "list of references" and the "=" operator has changed which object x[0] is pointing to. Under the same model,
myobj.a = 2
causes the value of a to change, as myobj.a is a reference which we are causing to point to "2". However, in numpy we can edit arrays in place using "fancy indexing":
import numpy as np
arr1 = np.array([1,2])
arr2 = np.array([0,0,0,0])
arr2[arr1] = np.array([1,1])
The value of arr2 is now [0,1,1,0]. This does not make sense to me, as under my previous model arr[arr1] is a reference pointing towards a "view" of arr2. How can assigning to this reference cause mutation in arr2? I'm not quite sure where the hole in my understanding is here.
I've found learning Python slightly frustrating as I can't seem to get a good "model of execution". I would love advice on how to get a better mental model of the language.