callbacks/functions pt 2?
greg andruk
meowing at banet.net
Wed May 26 09:06:43 EDT 1999
Jr King <n at n.com> writes:
> Yesterday, I got part of my answer about passing a python function to a
> C/c++ class. But, what was left out was passing the Python function to the
> c/c++ program, the docs did the same thing unless I skipped it.
This is covered in the 'Extended and Embedding' document, including a
code sample, in the section 'Calling Python Objects from C.'
What is mentioned in that section, but not directly shown, is that you
also need to add my_set_callback() to the method table you use with
Py_InitModule(), something like:
static PyMethodDef MyMethods[] = {
{"set_callback", my_set_callback, METH_VARARGS},
{"some_other_function", useful_function, METH_VARARGS},
{NULL, NULL}
};
To set the pointer so that C can use it, your Python script first
imports the C module, then passes the object to my_set_callback, like
so:
def thingy(aNumber):
return aNumber + 1
import my_c_module
my_c_module.set_callback(thingy)
This relationship borders on kinky, but hey, it works.
More information about the Python-list
mailing list