2014-11-24 55 views
0

我使用Python 3.4.2製作了這個python程序。 在開始時我定義了一個變量'best', 後來它可以在函數(我沒有忘記'全球最好')中進行更改。 每次改變它的印刷('新的最好',最好) 在程序結束時,它再次印刷最好的價值,但它不同於它上次改變的時間......這怎麼可能?Python全局變量本身改變....爲什麼?

大部分的代碼是不是這個重要的(我猜),但有什麼用最好做的線都標有##########

from time import clock 


size = [int(i) for i in input().split()] 
step = [int(i) for i in input().split()] 
clock() 

best = [[0 for i in range(size[1])] for i in range(size[0])] ########## 
best[0][0] = 1     ########## 
lenbest=1      ########## 

if step[0] == step[1]: 
    xstep = step[0] 
    moves = [[xstep,xstep],[-xstep,xstep], 
      [xstep,-xstep],[-xstep,-xstep]] 
    try: 
     best[xstep][xstep]  ########## 
     posToGo = [moves[0]] 
    except: None 

else: 
    moves = [[step[0],step[1]],[step[1],step[0]], 
      [-step[0],step[1]],[-step[1],step[0]], 
      [step[0],-step[1]],[step[1],-step[0]], 
      [-step[0],-step[1]],[-step[1],-step[0]]] 
    try: 
     best[step[0]][step[1]] ########## 
     posToGo = [moves[0]] 
    except: None 
    try: 
     best[step[1]][step[0]] ########## 
     posToGo.append(moves[1]) 
    except: None 

def attempt(board,posToGo,step): 

    currentMoves = [] 
    for pos in posToGo: 
     currentMoves.append([pos]) 
     for move in moves: 
      try: 
       x = pos[0]+move[0] 
       y = pos[1]+move[1] 
       if x>-1 and y>-1 and not board[x][y]: 
        currentMoves[-1].append([x,y]) 
      except:None 
    currentMoves.sort(key=len) 
    for move in currentMoves: 
     nboard = board 
     nboard[move[0][0]][move[0][1]] = step 
     attempt(nboard,move[1:],step+1) ########## Note that i'm calling a function within itself 
    if step>lenbest:    ########## 
     print("new best with pathlength",step-1,"after",clock(),"secconds") 
     global lenbest   ########## 
     lenbest = step    ########## 
     global best    ########## 
     best = board    ########## 
     print("in the def",best) ########## This one differs from... 


attempt(best,posToGo,2)    ########## 
print("at the end",best)   ########## ... this one. ??!! 

隨着輸入(舉例):

8 8 
1 2 

我得到這樣的輸出:

new best with pathlength 64 after 0.0017115690152521709 secconds 
in the def [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]] 
at the end [[1, 4, 3, 4, 3, 4, 5, 20], [4, 5, 2, 3, 4, 5, 4, 5], [3, 2, 13, 4, 3, 4, 7, 6], [14, 3, 4, 3, 10, 5, 4, 5], [3, 4, 3, 4, 5, 4, 5, 6], [4, 13, 4, 7, 4, 11, 6, 7], [13, 10, 13, 8, 7, 8, 7, 8], [14, 13, 8, 9, 10, 7, 8, 7]] 

所以這是不同的,而這只是更換一次(算上線說「新的最好的....」):「(

+0

'nboard = board'不會創建副本;它僅僅爲同一個對象創造了另一個參考。 – 2014-11-24 20:05:01

+0

嘗試使用返回值而不是全局值。 – wenzul 2014-11-24 20:06:24

回答

0

當一個變量的函數內部定義(如您在功能與attemptbest = board),這個變量將自動成爲當地的一個。這意味着在你的例子中,全局變量的值不會改變,儘管看起來如此。

爲了能夠寫入全局變量,在函數的開頭添加語句

global best 

另請注意,通過將board分配給best,您仍然只有一個數組 - 它現在被兩個變量引用。從模塊copy使用功能deepcopy

from copy import deepcopy 

你可以寫:

best = deepcopy(board) 

現在,輸出寫着:

('new best with pathlength', 64, 'after', 0.040691, 'secconds') 
('in the def', [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]]) 
('at the end', [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]]) 

即,存儲在best板的副本沒有改變。

+0

我嘗試了全局和非本地,但它仍然不工作...不是在函數的開始處,也不是在上面改變最好的行 – Wilcooo 2014-11-24 20:13:20