我使用python.org的教程 “将Python嵌入另一个应用程序”
如何检索调用C语言函数的Python模块的名称:
static int numargs=0;
/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":numargs"))
return NULL;
return Py_BuildValue("i", numargs);
}
static PyMethodDef EmbMethods[] = {
{"numargs", emb_numargs, METH_VARARGS,
"Return the number of arguments received by the process."},
{NULL, NULL, 0, NULL}
};
答案 0 :(得分:0)
我已经将python嵌入到我的C ++应用程序中。但需要动态添加模块。 与
Py_InitModule("module1", EmbMethods);
Py_InitModule("module2", EmbMethods);
相同的回调
static PyMethodDef EmbMethods[] = {
{"some_func", some_func, METH_VARARGS,""},
{NULL, NULL, 0, NULL}
};
需要这样的东西
static PyObject* some_func(PyObject *self, PyObject *args)
{
char *p;
if(!PyArg_ParseTuple(args, "s",&p))
return NULL;
std::cout<<self->module_name //<<<<<<<< self always == NULL , why?
return Py_BuildValue("i", numargs);
}
python脚本
import module1
print module1.some_func()
print module2.some_func()
预期产量: &#34;模块1&#34; &#34;模块2&#34;