I noticed that the augmented addition assignment operator (+=) behaves in an unexpected way within functions. See below:
import numpy as np
def f(array):
array += 1
return array
x = np.ones(5)
print(x)
y = f(x)
print(x)
This outputs:
[ 1. 1. 1. 1. 1.]
[ 2. 2. 2. 2. 2.]
So the array x is being modified within the function. Can anyone explain this? If instead of x += 1 I use x = x + 1, then the behavior is as expected (i.e. changes within the function do not affect the array x outside it.)
Obviously this is not causing my any problems (any more) - the solution is easy. Just looking to understand why this is happening.
Thanks!