views:

26

answers:

1

Assuming I have some embedded python code containing a function foo, what is the best way to get a reference to that function (for use with PyObject_CallObject)?

One way is to have some function register each function along with the function name either manually or through use of reflection. This seems like overkill.

Another way is to create a "loader" function that takes the function name and returns a reference to the function. Better, but I still have to register the loader function with C.

Am I missing a better way to do this with or without having to write any additional python code?

+1  A: 

Read the section in the documentation about embedding Python. I guess you have a reference to the module containing the function. At the end there is an example which shows how to get a function reference out of an object.

pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
    ...
}
Py_XDECREF(pFunc);
ebo