我是新來的Cython,並試圖通過在關鍵AI模塊中使用Cython代碼來加速我的kivy手機遊戲。我的代碼去如下:Cython編譯錯誤 - 分配前引用的局部變量
import numpy as np
cimport numpy as np
#not relevant parts
cdef np.ndarray posarr
cdef np.int poslast = 0
cdef np.int posidx = 0
def posarr_init(np.ndarray pawnpos, np.int act):
poslast = 0
# not relevant, but referencing poslast
#not relevant, but including function where poslast is referenced (not assigned)
def consider_pawn(np.int x, np.int y):
cdef np.int pact, posx, posy, resx, resy
cdef np.int p
cdef np.int found = 0
#not relevant
#in the code there is this line, posx and posy are local variables
posarr[poslast, posx, posy] = posarr[posidx, posx, posy]
這裏用Cython編譯過程中給了我這個錯誤:
Error compiling Cython file:
------------------------------------------------------------
...
pact = 1
if pact == 1:
#pawn is active, create child position
for posx in range(11):
for posy in range(11):
posarr[poslast, posx, posy] = posarr[posidx, posx, posy]
^
------------------------------------------------------------
position.pyx:98:34: local variable 'poslast' referenced before assignment
我可以看到,用Cython編譯錯誤以相同的順序,它們出現在代碼報道。我的問題是:
爲什麼Cython會考慮poslast的局部變量?
爲什麼它不認爲它是以前函數中的局部變量?
生成的C文件是空的,它只有一條消息,它不應該在裏面使用。有沒有辦法強制Cython將C代碼留在文件中,即使存在編譯錯誤?也許尋找到該文件將有助於我理解錯誤消息(即爲什麼這個變量被認爲是本地的)...
+1。 Cython還支持'nonlocal'關鍵字(Python 3) – jfs