我正在关注这个关于将 c++ 扩展到 python 的文档。就那里演示的内容而言,PyObject *spam_system(PyObject *self, PyObject *args) {...}
传递给数组时工作正常,PyModuleDef
但如果我想向 中添加更多参数spam_system
,则会导致以下无效对话
error: invalid conversion from ‘PyObject* (*)()’ {aka ‘_object* (*)()’} to ‘PyCFunction’ {aka ‘_object* (*)(_object*, _object*)’} [-fpermissive]
举个例子,我想定义一个不带参数的方法,并返回一个PyLong_FromLong
from long
/int
PyObject test() {return PyLong_FromLong(1);}
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
{"test", test , METH_NOARGS,
//^ the error happens here
"test."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
如何定义一个不带参数或两个以上参数的方法?(添加两个参数可以test
正常工作)。