我正在寻找合并一些 cython 来加速我的代码。 我在 Jupyter 中运行 cython 代码时遇到问题。
单元格 1:
%%cython
cdef fuc():
cdef int a = 0
for i in range(10):
a += i
print(a)
单元格 2:
fuc()
错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-48-10789e9d47b8> in <module>()
----> 1 fuc()
NameError: name 'fuc' is not defined
但如果我这样做,效果很好。
%%cython
def fuc():
cdef int a = 0
for i in range(10):
a += i
print(a)
看起来 cdef 在 Jupyter 中的使用方式不同,我如何在 Jupyter notebook 中使用 cdef?
最佳答案
cdef
functions can only be called from Cython, not Python .文档说
Within a Cython module, Python functions and C functions can call each other freely, but only Python functions can be called from outside the module by interpreted Python code.
(已经声明“C 函数”由 cdef
定义,“Python 函数”由 def
定义。)
改用 Cython 中的 def
函数。它仍然由 Cython 编译。您仍然可以在 def
函数中使用 cdef
类型。
关于python - 在 Jupyter cdef 中运行 Cython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43725273/