2013-01-02 2 views
17

Cython 컴파일러에 param이 함수라는 것을 알리는 방법이 있습니까? 뭔가 같은Cython에서 함수 유형이 있습니까?

cpdef float calc_class_re(list data, func callback) 
+0

다른 모든 것이 실패하면 C 유형 정의에 피기 백 (piggyback) 할 수 있습니다. 더 나은, 순수한 Cython 방법이 있을지도 모른다. – delnan

+0

파이썬 함수 또는 c 함수를 의미합니까? 함수 서명이 알려지면 "delnan"에 의한 주석이 c에서 작동합니다. – shaunc

+0

'cdef' 또는'cpdef' 함수의 경우 C 스타일의 functype이 작동해야합니다. 'ctypedef (* my_func_type) (object, int, float, str)'와 비슷합니다. 순수 파이썬 함수에'object' 유형을 사용해야합니다. –

답변

27

자명 한해야 할 ..? :)

# 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 
관련 문제