【发布时间】:2016-08-07 20:46:32
【问题描述】:
在纯 Python 中定义函数时,调用 help 时可以看到其签名。例如:
>>> def hello(name):
... """Greet somebody."""
... print "Hello " + name
...
>>> help(hello)
Help on function hello in module __main__:
hello(name)
Greet somebody.
>>>
但是,在 C/API 中定义 Python 函数时,其签名缺少基本信息:
static PyObject*
mod_hello(PyObject* self, PyObject* args)
{
const char* name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
printf("Hello %s\n", name);
Py_RETURN_NONE;
}
static PyMethodDef HelloMethods[] =
{
{"hello", mod_hello, METH_VARARGS, "Greet somebody."},
{NULL, NULL, 0, NULL}
};
这会产生:
>>> help(hello)
Help on built-in function hello in module hello:
hello(...)
Greet somebody.
任何想法如何在 C/API 中将签名从 hello(...) 更改为 hello(name)?
【问题讨论】:
标签: python python-c-api