I am trying to Cython to speed up some class. But I still want the code to run in pure Python too.
How do I define an array in an class (code has been simplified)
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
And in the .pxd:
import cython
cdef class A
cdef int[8] S
cdef test(self)
But Cython complains on compilation:
Cannot convert Python object to 'int [8]'
cython-compiledpart to fully take advantage of Cython's speedup, you should alwayscdefyour increment variables as well:cdef k...