Bas*_*asj 7 python string cython
我想这样做将字符串传递给Cython代码:
# test.py
s = "Bonjour"
myfunc(s)
# test.pyx
def myfunc(char *mystr):
cdef int i
for i in range(len(mystr)): # error! len(mystr) is not the length of string
print mystr[i] # but the length of the *pointer*, ie useless!
Run Code Online (Sandbox Code Playgroud)
但正如评论中所示,这里没有按预期工作.
我发现的唯一解决方法是传递长度作为参数myfunc.这是对的吗?难道真的将字符串传递到用Cython代码最简单的方法?
# test.py
s = "Bonjour"
myfunc(s, len(s))
# test.pyx
def myfunc(char *mystr, int length):
cdef int i
for i in range(length):
print mystr[i]
Run Code Online (Sandbox Code Playgroud)