I have a function that calls another function. For some reason, the variable in the first function is updated by the second function. Is python passing a variable by ref as a default? Basically why is 'AA' updated in the second run? And how can I make it the same, just like the first run.
For simplicity, I created a short sample code that has the same issue. I have a function (test) that calls (test2). The variable 'AA' in 'test' is updated by 'test2' when I perform BB.append(list_position[0]).
The two function is below:
def test(A, childs):
for x in range(0,childs):
list_pos = [x]
AA = A
print("")
print("AA: ", AA)
print("list_position: ", list_pos)
test2(origin,list_pos)
def test2(B,list_position):
BB = B
list_len = len(BB)
print(list_position[0])
#BB.append(list_position[0])
Output of the first run (with last code commented out):
AA: []
list_position: [0]
0
AA: []
list_position: [1]
1
AA: []
list_position: [2]
2
AA: []
list_position: [3]
3
Output of the Second run (with last code uncommented):
AA: []
list_position: [0]
0
AA: [0]
list_position: [1]
1
AA: [0, 1]
list_position: [2]
2
AA: [0, 1, 2]
list_position: [3]
3