为什么方法要更改此 Class 的属性? (Python)
Why is method changing the attribute of this Class? (Python)
我在下面定义了一个 Snake class 以及一个 Move 方法,它应该根据一个动作计算头部的新位置,将这个元素附加到块数组到蛇的块数组中,然后然后 popleft 这个列表的第一个元素。
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)
下面是我 运行 程序时得到的一些示例输出。
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])])
我得到了上面的,但我期望下面是这样的:
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])])
我的方法如何更改双端队列中的值?
在python中,对象是引用。 blocks
双端队列中的值实际上是对 currentPosition
所引用的同一个 numpy 数组的引用,因为它们都是从 start_position
初始化的。如果您希望它们独立,请尝试复制 值本身,而不是引用 ,使用 Snake.__init__
:[=16] 中的内置 copy
函数=]
self.blocks = deque([start_position.copy()])
我在下面定义了一个 Snake class 以及一个 Move 方法,它应该根据一个动作计算头部的新位置,将这个元素附加到块数组到蛇的块数组中,然后然后 popleft 这个列表的第一个元素。
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)
下面是我 运行 程序时得到的一些示例输出。
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])])
我得到了上面的,但我期望下面是这样的:
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])])
我的方法如何更改双端队列中的值?
在python中,对象是引用。 blocks
双端队列中的值实际上是对 currentPosition
所引用的同一个 numpy 数组的引用,因为它们都是从 start_position
初始化的。如果您希望它们独立,请尝试复制 值本身,而不是引用 ,使用 Snake.__init__
:[=16] 中的内置 copy
函数=]
self.blocks = deque([start_position.copy()])