开发者

Get a C reference to an embedded python function by name?

开发者 https://www.devze.com 2022-12-16 16:18 出处:网络
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)?

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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号