Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将函数作为Cython类的类属性传递_Python_Cython - Fatal编程技术网

Python 将函数作为Cython类的类属性传递

Python 将函数作为Cython类的类属性传递,python,cython,Python,Cython,我试图定义一个Cython类,它接受一个函数作为\uuuu init\uuu函数中的class属性之一 我遵循并尝试了以下方法 ctypedef int (*f_type)(int, int) cdef class ClassWFunction: cdef f_type f def __init__( self, f_type f): self.f = f def doStuff(self, int x, int y): return

我试图定义一个Cython类,它接受一个函数作为
\uuuu init\uuu
函数中的class属性之一

我遵循并尝试了以下方法

ctypedef int (*f_type)(int, int)

cdef class ClassWFunction:
    cdef f_type f

    def __init__( self, f_type f):
        self.f = f

    def doStuff(self, int x, int y):
        return self.f(x, y)

cdef int example_f(int a, int b):
    return a*b

classInstance = ClassWFunction(example_f)
classInstance.doStuff(2, 4)
这给了我一个错误:

Cannot convert 'int (int, int)' to Python object
我还阅读了Cython的文档,并尝试了以下方法:

cdef class f_type:
    cpdef int evaluate(self, int a, int b) except *:
        return 0

cdef class ClassWFunctionV2:
    cdef f_type f

    def __init__( self, f_type f):
        self.f = f

    def doStuff(self, int a, int b):
        return self.f.evaluate(a, b)


cdef class example_f(f_type):
    cpdef int evaluate(self, int a, int b) except *:
        return a*b    


classInstance = ClassWFunctionV2(example_f)
classInstance.doStuff(3, 4)
这给了我一个
类型错误

TypeError: Argument 'f' has incorrect type (expected _cython_magic_9f51dc40b83b28506fce9fb260a84618.f_type, got type)
希望有一种方法可以让我的第一次尝试成功,因为第二种方法有很多我不太理解的样板文件! 谢谢你的时间

--------编辑----------

以这种方式编写代码有两个要点:

1) 在实例化类时,可以灵活地更改
f
函数。 2)
f
函数在许多类方法中得到重用,因此我想将其分配给
self

在我的纯python代码中,我做了如下事情

def f1(x): return x**2
def f2(x): return x**3

cl1 = ClassWFunction(f1)
cl2 = ClassWFunction(f2)
然后继续处理这些类。然而,我不是100%确定这是最好的方法,所以请随意提出不同的方法

--------编辑2----------

作为一个不太灵活但(希望如此!)更容易的选择,我尝试将函数硬编码到类中。这将符合上述目标(2),即使它不符合目标(1)

考虑(这也会给出一个错误)


您想传入Python函数还是C函数?在Cython中实例化类并传递C函数有点奇怪。通常您的扩展类将由使用它的Python代码实例化。@BrenBarn:在我的代码中,
示例\u f
或多或少是关键的,所以我想传递一个cythonized函数。如果不可能的话,我愿意传递一个Python函数。您打算如何在Python空间中使用这段代码?比如,如果你只是在用Cython编写一个静态函数,你不能直接将它添加到类中吗?我添加了一个编辑来解决你的问题——我希望我理解正确。如何直接添加函数?在最初的示例中,您传递了一个C函数(使用
cdef
创建)。在编辑中,传递一个Python函数。你是说你想两者都支持?一般来说,
cdef
函数只能从C/Cython访问,因此如果您创建一个
cdef
函数,则无法使用Python代码将其传入。关于您链接的问题的评论建议,如果您希望允许传入Python函数,则应使用
对象
类型。
ctypedef int (*f_type)(int)
cdef class ClassWFunction:
    cdef f_type example_f
    cdef double x

    def __init__( self, int x):
        self.x = x
        # Tried with a python function too
        cdef int example_f(int z):
            return x*z
        self.example_f = example_f

    def doStuff(self, int x, int y):
        return self.example_f(x)*y

classInstance = ClassWFunction(3)
classInstance.doStuff(2, 4)