Hello Python/iPython users.
I have found a weird behavior of python using numpy arrays. I found a solution to the problem myself, but I'd love to get an explanation. Thanks in advance.
Here's the problem: Using ipython I create an numpy array a and a copy of a, called b:
import numpy as np
a=np.zeros(5)
b=a
However, b seems to be rather the identity of a and not a copy since changing b changes a as well.
b[0]=1
a
array([ 1., 0., 0., 0., 0.])
The solution is to use b=a.copy()
rather than b=a
, but I'd like to understand why this is the case in python. I'm quite familiar with Matlab,R and Fortran and never ran into a problem like this before. Why would someone want to have a second name for the same data instead of a copy of this vector? Just some python-specific syntax thing or is there more to understand?