为什么当我没有对其进行编程时,全局变量的值被更改(奇怪的行为)?



在开始之前,我想说我找到了解决这个问题的方法,我只想知道为什么会发生这种情况。

好的,我尽量简单地解释一下。

我正在写一个国际象棋游戏,我所做的是创建了一个嵌套列表,其中第三个值要么是None类型,要么是一张图片的名称(例如"bq.png"),它附着在一块黑皇后上。不要问我为什么这么做,这只是我下棋的方式。

但为了让我把它映射出来,我需要一个列表,其中所有None类型的第三个值都是这样的:

PROTO_BOARD=[
[0,0,None],  [39,0,None],   [79,0,None], [119,0,None],    [159,0,None],   [199,0,None],   [239,0,None],   [279,0,None],
[0,40,None], [39,40,None],  [79,40,None],[119,40,None],   [159,40,None],  [199,40,None],  [239,40,None],  [279,40,None],
[0,80,None], [39,80,None],  [79,80,None], [119,80,None],  [159,80,None],  [199,80,None],  [239,80,None],  [279,80,None],
[0,120,None],[39,120,None], [79,120,None],[119,120,None], [159,120,None], [199,120,None], [239,120,None], [279,120,None],
[0,160,None],[39,160,None], [79,160,None],[119,160,None], [159,160,None], [199,160,None], [239,160,None], [279,160,None],
[0,200,None],[39,200,None], [79,200,None],[119,200,None], [159,200,None], [199,200,None], [239,200,None], [279,200,None],
[0,240,None],[39,240,None], [79,240,None],[119,240,None], [159,240,None], [199,240,None], [239,240,None], [279,240,None],
[0,280,None],[39,280,None], [79,280,None],[119,280,None], [159,280,None], [199,280,None], [239,280,None], [279,280,None]
]

PROTO_BOARD是一个全局变量,有了它,我有了另一个名为current_BOARD的全局变量,当我第一次创建它时它是空白的。现在。。。

global current_board,PROTO_BOARD,PICS
current_board=PROTO_BOARD

for piece in self.all_pieces:
x=piece.rect.x
y=piece.rect.y
for i in current_board:
if i[0]==x and i[1]==y:
location_1=current_board.index(i)
location_2=PICS.index(piece.picture)
current_board[location_1][2]=PICS[location_2]

以上内容不言自明。如果图片与实际工件的x和y坐标匹配,则可以更改列表中的第三个元素(因此,如果在坐标(0,0)上有一个黑色rook,则current_board中的第一个元素将变为0,0,"br.png")。但以上的主要问题是,PROTO_BOARD将变成current_BOARD变成的任何东西,这意味着当它的所有第三个值都是Nones时,它可能会有类似"bq.png"的东西。

现在,如果我这样做,我的代码可以正常工作:

current_board=[
[0,0,None],  [39,0,None],   [79,0,None], [119,0,None],    [159,0,None],   [199,0,None],   [239,0,None],   [279,0,None],
[0,40,None], [39,40,None],  [79,40,None],[119,40,None],   [159,40,None],  [199,40,None],  [239,40,None],  [279,40,None],
[0,80,None], [39,80,None],  [79,80,None], [119,80,None],  [159,80,None],  [199,80,None],  [239,80,None],  [279,80,None],
[0,120,None],[39,120,None], [79,120,None],[119,120,None], [159,120,None], [199,120,None], [239,120,None], [279,120,None],
[0,160,None],[39,160,None], [79,160,None],[119,160,None], [159,160,None], [199,160,None], [239,160,None], [279,160,None],
[0,200,None],[39,200,None], [79,200,None],[119,200,None], [159,200,None], [199,200,None], [239,200,None], [279,200,None],
[0,240,None],[39,240,None], [79,240,None],[119,240,None], [159,240,None], [199,240,None], [239,240,None], [279,240,None],
[0,280,None],[39,280,None], [79,280,None],[119,280,None], [159,280,None], [199,280,None], [239,280,None], [279,280,None]
]

但我认为让current_board等于PROTO_board会更容易,但这导致了PROTO_board的值发生了变化。这和for循环有关,但我甚至并没有花太多时间PROTO_BOARD,所以为什么它的值会改变呢?这是某种虫子吗?

在Python中对对象执行A=B操作时,不会生成副本。。。你做了一个"参考"。

参见此示例:

>>> a = [1,2,3]
>>> b = a
>>> b[1]=5
>>> a
[1, 5, 3]
>>> 

现在你明白发生了什么吗?

相关内容

  • 没有找到相关文章

最新更新