目前我正在尝试 Cython 并想看看,如果我可以用这种语言编写我的整个项目,不幸的是我遇到了很大的问题。
我有一个 C 库,可以将其称为“lib.c”,从中导入一个函数:
cdef extern from 'lib.c':
double compute(double params[7], double IN[2])
现在我想将此函数分配给不必从 Python 访问的任意变量:
K = compute
这在 Python 中完全没有问题,但是在编译时我得到了错误
无法将“double (double *, double *)”转换为 Python 对象。
用 Cython 函数包装 C 函数也不起作用。如果我这样做
cdef double _K_wrap(double params[7], double IN[2]):
return compute(params, IN)
K = _K_wrap
我犯了同样的错误。
任何关于如何将 C 函数分配给 Python/Cython 变量的建议将不胜感激。提前致谢!
编辑:
好的,我刚刚尝试了hivert的建议:
ctypedef double (*Func)(double params[7], double IN[2])
cdef class FunctionHandler:
cdef Func K
def __cinit__(self, Func f):
self.K = f
def __call__(self, double params[7], double IN[2]):
return self.K(params, IN)
K = FunctionHandler(compute)
这会导致相同的错误。可能我在 __call__ 方法中做错了。