Cython 中是否有任何类型的函数? [英] Is there any type for function in Cython?

查看:23
本文介绍了Cython 中是否有任何类型的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以告诉 Cython 编译器 param 是函数.类似的东西

Is there any way to tell Cython compiler that param is function. Something like

  cpdef float calc_class_re(list data, func callback)

推荐答案

应该不言自明..?:)

Should be self-explanatory..? :)

# 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

这篇关于Cython 中是否有任何类型的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆