I would like to understand why I don't delete the array in spite of function call in case of Numpy?
In the case of the function call mul2 exactly what should happen happens. But if I try exactly the same with mul, I have a kind of reference to the original array.
How can I solve the problem? Do I have to "copy" the array first and then put it into the function? Or can I make a copy in the function and then delete it? What would be better here?
import numpy as np
def mul(h):
#g = np.zeros([h.shape[0],h.shape[0]])
g = h
g[:,0] *= 5
print(g)
def mul2(h):
h *= h
print(h)
a = np.array([[1,2,3,4], [2,3,4,5], [3,4,5,6]])
for i in range(2):
mul(a)
for i in range(2):
mul2(5)
The results are:
[[ 5 2 3 4]
[10 3 4 5]
[15 4 5 6]]
[[25 2 3 4]
[50 3 4 5]
[75 4 5 6]]
25
25
but from the behavier of mul2 i expect this as the solution:
[[ 5 2 3 4]
[10 3 4 5]
[15 4 5 6]]
[[ 5 2 3 4]
[10 3 4 5]
[15 4 5 6]]