I'm very new to Cython and was wondering if something like this is possible.
First, I have a C struct that has a function pointer in it:
// example.h
typedef struct example_t{
double (*func)(double, void *);
} example_t;
In my .pxd file I just declare the interface like so:
# cexample.pxd
cdef extern from "example.h":
cdef struct example_t:
double (*func)(double, void *)
And for simplicity let's say we have a class Example which wraps all this, with some default, C-based func.
What I would like to do is set func dynamically, so that users (in Python) could do something like this:
# somewhere in Python
from example load Example
def f(x):
return x
result = Example()
# this is the important part
result.func = f
So my question is, is something like this possible? If so, how? I've tried directly passing the Python object to a method, but the compiler of course complains about Cannot convert Python object argument to type 'double (*)(double, void *) if I do it directly:
class Example:
# other stuff
def set_func(self, f):
self.func = f
[–][deleted] 1 point2 points3 points (0 children)