如何通过C-API的名称调用python函数? [英] How to call a python function by name from the C-API?

查看:110
本文介绍了如何通过C-API的名称调用python函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从c-api中,我想按名称调用python函数.然后,我将使用一系列python对象作为参数来调用该函数.

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.

在Python文档中,我不清楚如何从主python解释器中获取"Callable"对象.

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. 从函数获取地址
  2. 以我的PythonObject作为参数调用该函数.

我正在使用Python 2.x系列进行开发.

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

推荐答案

基本上,您使用Python C API获取该函数所在的模块,然后在模块字典中查询该函数.与您的Python代码从某处调用函数时,Python运行时内部所做的大致相同.

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.

要查看的来自API的相关函数是PyImport_ImportModulePyModule_GetDictPyDict_GetItemPyObject_CallXXX系列函数.

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

这篇关于如何通过C-API的名称调用python函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆