我正试着用Cython来加速一些课程。但我仍然希望代码也能在纯Python中运行。
如何在类中定义数组(代码已经简化)
import cython
class A:
def __init__(self):
if cython.compiled:
# This will work in Cython
for k in len(self.S):
self.S[k]=k
else:
# This will work in interpreter
self.S=range(8)
def test(self):
self.S[0]+=1在.pxd中:
import cython
cdef class A
cdef int[8] S
cdef test(self)但是Cython抱怨编译:
Cannot convert Python object to 'int [8]'发布于 2013-12-19 07:13:26
我终于开始工作了:
import array
class A:
def __init__(self):
# This will work in Cython
self.S=array.array("l", range(8))
def test(self):
self.S[0]+=1和.pxd:
cimport cpython.array
cdef class RC4:
cdef int [:] S
cdef int test(self)发布于 2013-12-18 18:17:48
这是因为语法错误,更像是:
cdef int S[8]而且,没有必要使用import cython。
这实际上是在cython文档一开始就定义的。
https://stackoverflow.com/questions/20665249
复制相似问题