On Mar 9, 2010, at 4:01 PM, Greg Ewing wrote:

> Ondrej Certik wrote:
>
>> cdef double call_double_func(double x, void* ctx):
>>   return (<DoubleFunction>ctx).evaluate(x)
>
> Not sure about the specifics of ODE solving, but speaking
> about callbacks in general, it suffices to be able to
> pass an arbitrary callable object from Python.
>
> Any required context can be encapsulated by using a bound
> method or nested function as the callback. There is no
> need to require the Python code to provide an object with
> an "execute" method or anything like that.

The issue here is speed. Using a bound method, nested function, or  
other arbitrary Python callable enforces Python calling semantics  
(METH_KEYWORDS for __call__), which for a double -> double function  
that's going to be called thousands or millions of times in the course  
of solving differential equations is way to much overhead. Hence a  
special class with a cdef method. Also, we're taking about wrapping a  
C api that takes a (double*)(double, void*) and void* context, so it's  
a natural thing to do.

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to