【发布时间】:2014-02-21 15:58:09
【问题描述】:
我是 Cython 的新手,并试图通过在关键 AI 模块中使用 Cython 代码来加速我的 kivy 手机游戏。我的代码如下:
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 代码留在文件中?也许查看这个文件可以帮助我理解错误消息(即为什么这个变量被认为是本地的)......
【问题讨论】:
标签: python numpy compiler-errors cython