Can we pass a reference of python method as an argument to C. This method is a callback method which will be executed after the C code has been executed.
Asked
Active
Viewed 75 times
1 Answers
1
In the Python C-API, Python objects are simply PyObject
references. PyObject
references can be called using PyObject_Call (if you want to have more descriptive errors, you can call PyCallable_Check, first.)
Assuming you've extended a module using the API you would have a method as follows:
bool call_method(PyObject *method)
{
PyObject *args = PyTuple_New(0);
if ( NULL == PyObject_Call(method, args, NULL) )
{
// Method call failed
return false;
}
return true;
}
Then, in Python, you call the method using the following:
import my_module as bla
bla.call_method(myClass.myMethod)

Jimmy Thompson
- 1,004
- 7
- 17
-
Thanks for your response. Is the same kind of thing possible in JNI as well ? – Chaitanya May 20 '13 at 09:06
-
1I've never used the Java Native Interface before, a quick search returned this though: http://stackoverflow.com/questions/6746078/implement-callback-function-in-jni-using-interface – Jimmy Thompson May 20 '13 at 10:51