Thanks Thomas,
> You can actually mix instance methods (class methods with self pointers)
> and regular methods from the stand point of C. I do all the time for the
> reasons you describe. When you use the C api for python, there is a
> function that calls python functions. The structure / object for those
> python functions has information in it like the self pointer. It's a
> different concept than a c function pointer.
I think the hardest part about using ctypes is the mixing of concepts
of C together with the concepts of Python. The documentation uses
callbacks in qsort that is a "regular" function, but it doesn't point
out that you can use instance methods too. I can't help but wonder if
a sample using a class would go a long way for other newbies.
> So to think of it another way,
> when you let ctypes wrap a python function (even an instance one with a
> self pointer) as a C function pointer - ctypes is really putting a shim
> function in the middle.
Ah. Shim function.
> I don't have access to the right computers in front of me, but I'll hunt
> down an example if you need one when I do.
I think we should put those examples in the python docs, honestly. I
spent a few days stuck on trying to figure out how to get self in
callback, when I didn't have to do anything special at all! I was
sitting there counting the number of parameters and thinking that C
would see five parameters in a callback that had self embedded when it
only needed four.
I realize now that ctypes is designed with the"self is just magically
there" ethos in mind. But it might help to have it pointed out from
the get-go. I can't believe I'm the first not to get my head wrapped
around it ...
Anyway, it all works now and my students shall hopefully be voting
from their desks, while the software behind the scenes is accepting
their votes via my python-based callback and putting the information
automagically into encapsulated classes.
Cheers and thanks to all for the help,
-Adam
|