我从C++调用python函数。我想知道是否可以确定参数的数量和这些参数的名称。我已阅读链接How to find the number of parameters to a Python function from C?,但我不太明白。从C++中查找python函数参数
我有这个C++函数,它调用pyFunction.py函数'add'。 'add'需要两个参数并返回总和。
static float CallPythonFunc(float *parameters)
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;
float ret;
// Initialize the python interpreter
Py_Initialize();
// Make sure we are getting the module from the correct place
// ### This is where we will put the path input
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"/Developer/IsadoraSDK/IsadoraDemoMathFunction/\")");
// Build the name object
// ### This is where we will put the function input
pName = PyString_FromString("pyFunction");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is a borrowed reference
pFunc = PyDict_GetItemString(pDict, "add");
//
// Somehow get the number of arguments and possible the arguments names from 'add'
//
if (PyCallable_Check(pFunc))
{
// Set the number of arguments
// This is where I would like to pass in number of arguments
pArgs = PyTuple_New(2 /*number of arguments*/);
//
// Instead of the following if I had the arguments I could loop through them
// and pass the correct number in
//
// Argument 1
pValue = PyFloat_FromDouble((double)parameters[0]);
PyTuple_SetItem(pArgs, 0, pValue);
// Argument 2
pValue = PyFloat_FromDouble((double)parameters[1]);
PyTuple_SetItem(pArgs, 1, pValue);
// Make the call to the function
pValue = PyObject_CallObject(pFunc, pArgs);
// Set return value
ret = (float)PyFloat_AsDouble(pValue);
// Clean up
Py_DECREF(pArgs);
Py_DECREF(pValue);
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return ret;
}
我真的不熟悉C/C++,所以任何帮助都会很有帮助。感谢大家的时间!编辑: 所以像下面这样的东西?
PyObject *tuple, *arglist;
tuple = PyObject_CallMethod(pFunc,"inspect.getargspec","add");
arglist = PyTuple_GetItem(tuple,0);
int size = PyObject_Size(arglist);
谢谢你的回应。我还编辑了我的帖子,提供了一个后续问题 – konbanwa
除了你在编辑中的内容之外,你还需要包含像'size + =(PyTuple_GetItem(tuple,1)== Py_None?0:1)+ PyTuple_GetItem(tuple,2)== Py_None?0:1);'来说明'*'和'**'参数的可能性。 (如果你不认识使用的语法,它被称为三元运算符,类似于在线的'if ... else'语句。) – JAB
哦,实际上,请检查我的更新答案,以了解您可能想要执行的操作。 – JAB