Search code examples
pythonobjectmethodsdeque

Why is method changing the attribute of this Class? (Python)


I have a Snake class defined below along with a method Move which is supposed to calculate the new position of the head according to an action , append this element to the blocks array to the snake's block array and then popleft the first element of this list.

class Snake:
    actions = [np.array([-1, 0]), np.array([1, 0]), np.array([0, -1]), np.array([0, 1])]

    def __init__(self, start_position, start_direction_idx):
        """

        :type start_position: 2x1 numpy Array
        """
        self.startPosition = None
        self.alive = True
        self.direction = start_direction_idx
        self.length = 1
        self.currentPosition = start_position
        self.blocks = deque([start_position])
        self.blockAtEndofTail = None

  def move(self, action):
        if self.isMovingBackwards(action):
            action = self.direction

        else:
            self.direction = action
        print('Blocks before anything', self.blocks)
        print('Current position before action',self.currentPosition)

        self.currentPosition += self.actions[self.direction]

        print('Current position after action', self.currentPosition)
        print('Blocks before pop',self.blocks)

        self.blockAtEndofTail = self.blocks.popleft()
        print('Blocks after pop', self.blocks)

        self.blocks.append(self.currentPosition)
        print('Blocks after append', self.blocks)

        print(self.blocks)

Here is some sample output below that I get when I run the program .

Blocks before anything deque([array([10, 10])])
Current position before action [10 10]
Current position after action [ 9 10]
Blocks before pop deque([array([ 9, 10])])
Blocks after pop deque([])
Blocks after append deque([array([ 9, 10])])
deque([array([ 9, 10])])

I got the above but I expected this below:

Blocks before anything deque([array([10, 10])])
Current position before action [10 10]
Current position after action [ 9 10]
Blocks before pop deque([array([ 10, 10])])
Blocks after pop deque([])
Blocks after append deque([array([ 9, 10])])
deque([array([ 9, 10])])

How is the value in the deque being changed by my method?


Solution

  • In python, objects are references. the value in the blocks deque is actually a reference to the same numpy array that currentPosition is referencing, since they both were initialized from start_position. If you want them to be independent, try copying the value itself, and not the reference, using the built-in copy function in Snake.__init__:

    self.blocks = deque([start_position.copy()])