0
Class A:
    def somefunction(self,somelist):

        print(str(somelist)) #Prints ["1","#2","3","#4"]

        b = B()
        filterlist = b.remove_tagfromarray(somelist)  

        print(str(somelist))  #Prints ["1","3"] #<<<<<WHY IS THIS?!!!!!!
        print(str(y))         #Prints ["1","3"]

remove_tagfromarray(x) takes in an array and returns an array without hashtags. However, after doing the code above it also alters the ORIGINAL array which was never operated on or referenced in the function called in the other class. I'm extremely confused. Even introducing a new variable doesn't seem to work.

Class A:
    def somefunction(self,somelist):

        savedarray = somelist
        print(str(somelist)) #Prints ["1","#2","3","#4"]

        b = B()
        filterlist = b.remove_tagfromarray(somelist)

        print(str(savedarray))  #Prints ["1","3"]  WHY!?!?!?!?
        print(str(filterlist))  #Prints ["1","3"]

as far as the function is concerned, it takes in the list as an argument, copies it to another "returnarray" variable, which is fiddled with until it is returned in the end. Mind you it works as I'm getting correct results, but somehow that function alters the original variable. Am I missing something here? Obviously somelist or savedarray (in the second example) should return the unaltered array.

def remove_tagfromarray(self,somelist,bannedchar="#"):
        popque = []
        popcount = 0
        n=0
        returnarray=somelist
        for word in returnarray:              
            if (bannedchar in word)):  
                popque.append(n)        
            n+=1

        for target in popque:            
            returnarray.pop(target-popcount)   
            popcount = popcount + 1

        return returnarray
Agneum
  • 727
  • 7
  • 23
  • 1
    It might possibly be helpful if you actually showed us the code of `remove_tagfromarray()`. That said, Python likes to pass objects by reference, so `savedarray = somelist` just creates a new reference to the same `list`. – TigerhawkT3 Apr 10 '15 at 19:02
  • Sorry, I added the code that removes the tag from the list (yes, I'm aware there might be a much better way to do this). It might very well be the pass objects by reference. If so, what can I do instead? I'll try reading the other thread and doing the splice method of creating the copied list. – Agneum Apr 10 '15 at 19:08
  • Yes, creating a copy of the list, either in `filterwords()` or before you call it, should do it. The slice operator works for this: `returnarray = somelist[:]` – eldarerathis Apr 10 '15 at 19:10
  • 1
    Read the linked question and its answers. It describes the exact same problem. – TigerhawkT3 Apr 10 '15 at 19:10
  • Yep, `returnarray = somelist[:]` fixed it. Thanks for helping out a newbie like me :) PS: How did you find the duplicate question so fast, seems like it would be hard to find an exact problem. – Agneum Apr 10 '15 at 19:16
  • 1
    The beginner gotchas produce frequent questions and can therefore be recognized and resolved pretty quickly. – TigerhawkT3 Apr 10 '15 at 19:21

2 Answers2

0

Simply put, lists can be passed around by reference. Somewhere in code outside the listed code these lists are getting linked together by one referencing the other or something of a similar nature.

Tristan Maxson
  • 229
  • 4
  • 15
0
returnarray=somelist

Python passes everything by reference. This line makes the two names returnarray and somelist refer to the same list. Everything you do to returnarray will be reflected in somelist.

Make a copy instead:

returnarray=somelist[:]
nneonneo
  • 171,345
  • 36
  • 312
  • 383