2017-05-01 52 views
3

我正在寻找合并一些cython来加速我的代码。 我在Jupyter中遇到了运行Cython代码的问题。在Jupyter cdef运行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笔记本中使用cdef?

回答

5

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类型。

0

how could I use cdef in Jupyter notebook?

试图改变CDEFcpdef