I am slowly trying to understand the difference between view
s and copy
s in numpy, as well as mutable vs. immutable types.
If I access part
Yes, it is the same object. Here's how you check:
>>> a
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
>>> a2 = a
>>> a[b] = 1
>>> a2 is a
True
>>> a2
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Assigning to some expression in Python is not the same as just reading the value of that expression. When you do c = a[b]
, with a[b]
on the right of the equals sign, it returns a new object. When you do a[b] = 1
, with a[b]
on the left of the equals sign, it modifies the original object.
In fact, an expression like a[b] = 1
cannot change what name a
is bound to. The code that handles obj[index] = value
only gets to know the object obj
, not what name was used to refer to that object, so it can't change what that name refers to.