python - Cython 中是否有任何类型的函数?

标签 python callback cython

有没有办法告诉 Cython 编译器 param 是函数。类似的东西

  cpdef float calc_class_re(list data, func callback)

最佳答案

应该是不言自明的......? :)

# Define a new type for a function-type that accepts an integer and
# a string, returning an integer.
ctypedef int (*f_type)(int, str)

# Extern a function of that type from foo.h
cdef extern from "foo.h":
    int do_this(int, str)

# Passing this function will not work.
cpdef int do_that(int a, str b):
    return 0

# However, this will work.
cdef int do_stuff(int a, str b):
    return 0

# This functio uses a function of that type. Note that it cannot be a
# cpdef function because the function-type is not available from Python.
cdef void foo(f_type f):
    print f(0, "bar")

# Works:
foo(do_this)   # the externed function
foo(do_stuff)  # the cdef function

# Error:
# Cannot assign type 'int (int, str, int __pyx_skip_dispatch)' to 'f_type'
foo(do_that)   # the cpdef function

关于python - Cython 中是否有任何类型的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124049/

相关文章:

python - 二值图像python中多个 Blob 的区域

android - 带有扩展 EditText 的回调

swift - placesClient - 正确使用 sharedClient 方法?

javascript - 是否可以将参数传递给链式匿名函数回调?

python - 如何使 Cython 比 Python(没有 Numpy)更快地添加两个数组?

python - 排除导致 GAE 服务器重启的文件

Python:在 .csv 文件中写入时删除特定字符

python - 带有 C 指针的 Pickle Cython 类

function - 我应该使用 def、cdef 或 cpdef 定义我的 Cython 函数以获得最佳性能吗?

python - Pyserial无法读取数据,但minicom工作正常