0

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
user1179317
  • 2,693
  • 3
  • 34
  • 62
  • test2(origin,list_pos) should be test2(AA,list_pos) – user1179317 Jan 27 '16 at 21:47
  • Also, please don't submit piece-meal code snippets like this which are incomplete. Instead, include an [MVCE](http://stackoverflow.com/help/mcve) which demonstrates the problem. – Jonathon Reinhart Jan 27 '16 at 21:48
  • Okay, `AA = A` and `BB = B` don't really seem to do anything, since they just reference the parameter that was passed in – OneCricketeer Jan 27 '16 at 21:48
  • Yea it doesn't do anything, I was just trying to find out why updating BB changes AA. – user1179317 Jan 27 '16 at 21:49
  • 3
    Because `BB` *is* `AA`. `BB = B = AA = A` – OneCricketeer Jan 27 '16 at 21:50
  • Shouldn't 'A' be a different variable? If not how can I keep that variable in 'test' unchanged, when updating BB – user1179317 Jan 27 '16 at 21:52
  • @user1179317 They're different variables, but both variables refer to the same list. See the duplicate question. – Barmar Jan 27 '16 at 21:53
  • 1
    python use reference to the same place in memory instead of copy/clone/duplicate values and put it in new place - you have to copy/clone/duplicate values on your own - for example `BB = B[:]` or in Python3 `BB = B.copy()` – furas Jan 27 '16 at 21:56
  • Jonathon, thanks. It is a duplicate of that question. I guess I didn't realize that's how python works, that it always reference to the same place in memory – user1179317 Jan 27 '16 at 22:01
  • 1
    The key thing to understand is *mutable* objects (sets, lists, dicts) vs *immutable* objects (strings, integers, tuples etc). If you can get your head around the difference you'll generally avoid this sort of confusion. – Tom Dalton Jan 27 '16 at 22:31

0 Answers0