【发布时间】:2021-02-20 17:34:17
【问题描述】:
我有一个使用 Python C API 编写的模块:
static PyObject* my_func1(PyObject* /*self*/, PyObject* args)
{
Py_RETURN_NONE;
}
static PyObject* my_func2(PyObject* /*self*/, PyObject* args)
{
Py_RETURN_NONE;
}
static PyMethodDef methods[] = {
{"my_func1", (PyCFunction)my_func1, METH_VARARGS, ""},
{"my_func2", (PyCFunction)my_func2, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* sentinel */
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, my_module, NULL, -1, methods, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_my_module(void){
return PyModule_Create(&moduledef);
}
作为用户可以传递函数的参数之一,例如:
my_func1(my_func2)
如何使用 Python C API 在my_func1 内部检测到用户传递的参数是一个指向my_func2 的函数?
【问题讨论】:
标签: python c python-3.x python-c-api