The goal is to update a list by function call which takes two arguments, one being the list to be modified.
In the following example, j is the list to be modified by function f():
import numpy as np
j = np.log(np.array([[0.3],[0.4]]))
k = np.log(np.array([[0.1],[0.3]]))
def f(a,b):
a = np.exp(a) - np.exp(b)
f(j,k)
print(j)
The expected solution was:
[[ 0.2]
[ 0.1]]
However, it produces :
[[-1.2039728 ]
[-0.91629073]]
which is the original value of j. That is, j was never modified.
But if the += is used instead with the -a trick,
def f(a,b):
a += -a + np.exp(a) - np.exp(b)
then the output is the expected solution.
Hence, the question: does the assignment operator overwrite argument reference in Python functions?
Is there an explanation for why j's behavior changes if = or += is used?