I am confused by the mutable variable in python. See the following example code:
In [144]: a=[1,2,3,4]
In [145]: b=a
In [146]: a.append(5)
In [147]: a
Out[147]: [1, 2, 3, 4, 5]
In [148]: b
Out[148]: [1, 2, 3, 4, 5]
Since list is mutable, when using append function, it works on the same memory. This is understandable to me. But the following code confuses me.
In [149]: import numpy as np
In [150]: a=np.random.randn(3,2)
In [151]: b=a
In [152]: a=a-1
In [153]: a
Out[153]:
array([[-2.05342905, -1.21441195],
[-1.29901352, -3.29416381],
[-2.28775209, -1.65702149]])
In [154]: b
Out[154]:
array([[-1.05342905, -0.21441195],
[-0.29901352, -2.29416381],
[-1.28775209, -0.65702149]])
Since the Numpy array variable is also mutable, when a=a-1, why the change didn't be made on the same memory that a refers to? On the other hand, a refers to a new memory with new values.
Why variable a didn't act similarly in the first example about appending a new value 5 in the list, a still refers to the same memory?