In the function "change(par)", does "par[:]" already make a local copy? It works for the list, why doesn't it work for the array?
import numpy
def change(par):
copy = par[:]
copy[0] = 123
def main():
L = [0, 0, 0]
print '\nL before: ' + str(L)
change(L)
print 'L after: ' + str(L)
A = numpy.zeros((1, 3))
print '\nA before: ' + str(A)
change(A[0])
print 'A after: ' + str(A)
if __name__ == '__main__':
main()
Output:
L before: [0, 0, 0]
L after: [0, 0, 0]
A before: [[ 0. 0. 0.]]
A after: [[ 123. 0. 0.]]
UPDATE
Thank you for pointing out "par[:]" is a shallow copy, and it doesn't work for array.
So how does "shallow copy" work in case of array structures? In the case of list, "shallow copy" copies the values, but when it turns into array, "shallow copy" just copies the references not the values?
How does "[:]" distinguish when to copy values and when to just copy references?