Reputation: 1573
Something I don't understand with Python 3.3.3 and NumPy:
from numpy import *
x1 = zeros(1)
x2 = x1
x1+=1
It turns out, this makes both the x1 and x2 variables [1], which I don't understand. If you instead do x1=x1+1, then I get x1 as [1] and x2 as [0], which is what I was after.
Upvotes: 3
Views: 94
Reputation: 545548
x2 = x1
makes both variables x2
and x1
refer to the same object.
x1+=1
changes the object underlying the reference x1
(and x2
).
Conversely, if you did x1=x1+1
then you create a new object (the result of x1+1
) and assign the result to x1
only, while x2
remains unchanged and refers to the original object.
This is unrelated to NumPy, by the way – it’s a consequence of the general way references work.
Upvotes: 6