On Apr 21, 2009, at 2:05 PM, Jean-Francois Moulin wrote:
>> I may be missing something about your use cases, but is there any
>> reason you're passing/storing function pointers all over the place
>> instead of just calling cdef methods on a cdef class? That would
>> probably make your code a lot cleaner and easier to understand (and
>> write).
>
> Well, that might be due to me being a newbie ;0)
> Let me briefly explain my problem and idea:
> I need to a apply a discrimination function on some data, depending on
> the structure of these data, the discri fn can take two forms
> (possibly
> more in the future).
> Rather than to use an if / else test at each iteration to branch to
> the
> relevant function, I do the test once at the beginning (first time
> I see the
> data) and then define via a pointer which fn to use. Same thing for
> the locate
> function, which can vary depending on the available data.
BTW, calling a function pointer can be more expensive than a
predictable branch, depends on what you're doing. In Python, of
course, it can make a huge difference.
> I hope this make sense, and when not, I am more then willing to
> learn of a
> better approach... I am learning the hard way!
Here's what I would do--I'd make a class that has cdef discrimination
and cdef locate functions, then subclass it for all the different
data structures you want to handle. You can then do
cdef DataHandlerClass f = [decide which one here]
for x in data:
y = f.locate(x)
...
locate can be a nice cdef (or even cpdef) function, with optional
arguments and all the other niceness that you've come to expect. I
also think this better encapsulates what you're trying to do, but
that's subjective of course.
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev