how to get "self" in callback function
Adam Morris <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
So the C library I'm using has a callback function that lets me define
a context argument, which is a c_void_p type.
I want the callback to call a class instance. I'm implemented the
callback on the Python as a static method, obviously because C doesn't
want 'self' as a parameter, and it works fine:
@staticmethod
def standard_vote_callback(handle, serialNumber, vote, context):
output("The dude at serial number {0} voted
{1}".format(serialNumber, vote))
if context:
output("Got the context: {0}".format(context))
return 1
When context is a simple integer, it works fine of course. But if want
to update state information of the class instance, but I don't have
access to "self." So my idea obviously is to send self as the context.
I tried sending context as a py_object(self) and get an integer in the
callback itself (which I presume is the address), but am unable to
resolve it. How do I take that integer and turn it into a legitimate
Python class?
I tried searching the internet but couldn't find much; I'm surprised
as I would have thought this would be a common need, given the power
of an object-oriented language like Python to work with C libraries.
Or is the solution so trivial that I'm overlooking the obvious?
Any ideas?
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit. See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo