2014-02-13 66 views
0

在我開始之前,我想說我找到了一個方法,我只想知道爲什麼發生這種情況。爲什麼當我沒有將它編程爲(奇怪的行爲)時,全局變量的值被改變了?

好的,我會盡量簡單地解釋一下。

我正在寫象棋遊戲,而我所做的是我創建了一個嵌套列表,其中第三個值將是None類型或圖片的名稱(例如「bq.png」),它遵守一塊 - 黑色女王。不要問爲什麼我這樣做,這只是我做我的國際象棋遊戲的方式。

但爲了讓我繪製了這一點,我需要在他們的第三個值的所有無類型列表,像這樣開始:

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)上有黑色車,則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的時間,所以爲什麼它的值會改變?這是一種錯誤嗎?

+0

當您在Python中使用對象執行'A = B'時,您不會複製...您創建一個「引用」。 – Floris

回答

0

當您在Python中使用對象執行A = B時,您不會創建副本......您創建「引用」。

請參見本示例:

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

現在你明白髮生了什麼事?

相關問題