How to call a python function by name from the C-API?

岁酱吖の 提交于 2020-01-15 03:29:26

问题


From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments.

It is not clear to me in the Python documentation how I would get a "Callable" object from the main python interpreter.

Any help appreciated in:

  1. Getting the address from the function
  2. Calling the function with my PythonObject's as arguments.

I'm using Python 2.x series for my development.


回答1:


Basically, you use the Python C API to get the module the function is contained in, then query the module dictionary for the function. That's more or less the same what the Python runtime does internally when your Python code invokes a function from somewhere.

Relevant functions from the API to look at are PyImport_ImportModule, PyModule_GetDict, PyDict_GetItem and the PyObject_CallXXX family of functions.



来源:https://stackoverflow.com/questions/6558957/how-to-call-a-python-function-by-name-from-the-c-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!