小编ksh*_*ni1的帖子

如何从 C 的 PyObject 类型的函数将值从 C 返回到 python?

我试图通过从 python 调用 C 文件来传递一个值,然后再次将该值从 C 返回到 python。

我的问题是如何做到这一点?可以使用返回Py_BuildValue(a+b)类型的东西吗?

#include <Python.h>

static PyObject *
hello_world(PyObject *self, PyObject *noargs)
{
   int a=5,b=10;

   return Py_BuildValue(a+b); //This is Errorus.
}


static PyMethodDef
module_functions[] = {
    { "hello_world", hello_world, METH_NOARGS, "hello world method" },
    { NULL }
};



PyMODINIT_FUNC
inittesty2(void)
{
    Py_InitModule("testy2", module_functions);
}
Run Code Online (Sandbox Code Playgroud)

c python python-c-api python-c-extension python-extensions

4
推荐指数
1
解决办法
2164
查看次数