17
Czy jest jakiś sposób poinformowania kompilatora Cythona, że param jest funkcją. Coś takiego, jakCzy istnieje jakiś typ funkcji w języku Cython?
Czy jest jakiś sposób poinformowania kompilatora Cythona, że param jest funkcją. Coś takiego, jakCzy istnieje jakiś typ funkcji w języku Cython?
Powinno być zrozumiałe ..? :)
# 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
Jeśli wszystko inne zawiedzie, prawdopodobnie możesz połączyć się z C# typedef. Może jednak istnieć lepszy, czysty sposób na Cythona. – delnan
Masz na myśli funkcję Pythona lub funkcję c? komentarz "delnan" zadziała dla c, gdy znany jest podpis funkcji. – shaunc
Dla funkcji 'cdef' lub' cpdef' powinien działać typ C-style. Podobnie jak 'ctypedef (* my_func_type) (object, int, float, str)'. Musisz użyć typu 'object' dla funkcji czystego Pythona. –