[Cython] cdef class understanding
Stefan Behnel
stefan_ml at behnel.de
Mon Mar 16 14:41:49 CET 2009
Martin Gysel wrote:
> it is a little bit more complex as my callback
> expects more than one argument
> to register:
> c_lib_call_reg_callback(mycontext, <c_cb_t>self.callback, <void*>self)
>
> and the callback (as plain c fuction)
> void callback(context *c, void *userdata)
>
> according to your code above, the first argument where the callback gets
> called is the first argument of the declared callback (in code above,
> cb(userdata) -> callback(self): userdata->self)
Yes. That's a special case that would have worked in your original example.
> now I'm asking myself what's the benefit to declare a c callback in the
> class rather then outside and and call another class method from there,
> like this:
> cdef void callback(context *c, void *userdata):
> doWhatHasToBeDone(c)
> anotherOperation(<a_c_struct_t *>(<Cobj>userdata).loop)
> itsDone((<Cobj>userdata).cbDone())
>
> what's the best way to do such things?
It's perfectly ok (and very common, due to signature constraints) to
define a callback function outside of a class and to call back into some
class instance from inside the callback (just as you do above). In many
cases, your callback context/user-data will be a Python object anyway. It
really just depends on what your callback does.
Stefan
More information about the Cython-dev
mailing list