저는 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]
을 여기에 사이 썬은 컴파일하는 동안 나에게 오류를 제공합니다
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은 로컬 변수를 poslast로 간주하나요?
왜 이전 함수의 로컬 변수로 간주되지 않았습니까?
생성 된 C 파일이 비어 있으며 내부에서 사용해서는 안된다는 메시지 만 있습니다. 컴파일 오류가 있어도 Cython에서 C 코드를 강제로 남겨 둘 수있는 방법이 있습니까? 어쩌면이 파일에 찾고 ... (이 변수가 지역으로 간주됩니다 즉 이유) 나 오류 메시지를 이해하는 데 도움 것
+1. Cython은'nonlocal' 키워드도 지원합니다 (Python 3). – jfs