-2
    def get_all_children(self):
    zero = self.current_coords[0]
    zero_row, zero_col = zero
    states = []
    # blank spot is not on top row(middle or bottom) so we move up!
    if zero_row > 0:
        swapRow = zero_row - 1
        tempState = copy.copy(self.current) # a list of list of strings
        tempState[zero_row][zero_col], tempState[swapRow][zero_col] = tempState[swapRow][zero_col], tempState[zero_row][zero_col]
        s = State(tempState)
        states.append(s)
    ##  blank spot is not on the bottom row(middle or top) so we move down!
    if zero_row < 2:
        swapRow = zero_row + 1
        tempState = copy.copy(self.current)
        tempState[zero_row][zero_col], tempState[swapRow][zero_col] = tempState[swapRow][zero_col], tempState[zero_row][zero_col]
        s = State(tempState)
        states.append(s)

I have a State class that contains a list of lists named 'current' and i'm trying to define a function that gets all the possible children(moves) of the current state. in the first IF statement, i create a COPY of the current variable(which is a list of lists) and store it in a 'tempState' list and then i try to swap values on that tempState, create a State object passing that in and then append that object to a list. All that works fine. The problem is when it get's to the 2nd IF statement. After i did the swap, It modified the original 'current' variable even though i created a copy! and I can't figure out why. I tried list(self.current), self.current[:], copy.cop(self.current). Please help

David
  • 45
  • 3

1 Answers1

2

You have to use copy.deepcopy(x) instead.

stanwise
  • 2,392
  • 1
  • 15
  • 21
  • thanks!, i didn't think to do that as i thought that was for if i wanted to copy objects with attributes. – David Nov 18 '16 at 22:29