Why do I have to .copy()
a numpy array (or list, though those don't have .copy()
but make for a good minimal example) when I want to get an independend copy of it, while I don't have to do so with a variable? What's the reason for this behaviour?
E.g. setting a = 1
and b = a
and then setting b = 2
does not change a
, whereas a=([0])
, b = a
and b[0]=1
also changes a
.
Edit:
It appears that this is a very common issue/question that can be asked in various ways, so I couldn't find it. For a quick "I just need to understand what's happening so that I can move on" I guess the answer (and the answers to the other same/similar questions) are good enough. For a better understanding I guess this link provided by @chthonicdaemon in the comments seems like a good starting point for me.
longer Background
In many situations I have to set up a numpy array with some starting values which then gets filled with other values while my program runs. I.e.
logger = np.zeros(5)
#various things happen
logger[0] = 1
#more stuff happens
logger[3] = 1
you get the idea.
But often it is desirable to have the base array preserved in order to be able to compare the results to it, so I just set up the base first and then make copies from it. So I would expect that I simply could set it up like this:
base = np.zeros(5)
logger = base
logger[0] = 1
so that
In [1]:base
Out[1]:array([ 0., 0., 0., 0., 0.])
In [2]:logger
Out[2]:array([ 1., 0., 0., 0., 0.])
However with the above array, they stay "connected", so that I get
In [1]:base
Out[1]:array([ 1., 0., 0., 0., 0.])
I can fix that by explicitly using
logger = base.copy()
but I'm wondering why I have to.