I have a Cython file called foo.pyx containing the following functions:
def add_one(int n):
cdef int m = n + 1
return m
cdef int c_add_one(int n):
return n + 1
I build this pyx file using cython -a foo.pyx and can then do:
>>> import foo
>>> foo.add_one(5)
6
>>> foo.c_add_one(5)
AttributeError: 'module' object has no attribute 'c_add_one'
So it looks like I can't call c_add_one from python. What are the advantages of declaring a function using cdef?
cdefdefines a function that can be accessed only from C. If you wantc_add_oneto be accessible from Python, you should usecpdefinstead. The advantage is ofcdefis to remove the overhead of the python layer when calling it.