When using the Python/C API to extend C++ to Python, should local C++ functions be declared as static?
#include <Python.h>
static PyObject* py_addNumbers(PyObject* self, PyObject* args) {
int a, b;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return nullptr;
}
int result = a + b;
return PyLong_FromLong(result);
}
PyMODINIT_FUNC PyInit_example(void) {
static PyMethodDef ModuleMethods[] = {
{ "addNumbers", py_addNumbers, METH_VARARGS, "Add two numbers" },
{ nullptr, nullptr, 0, nullptr }
};
static PyModuleDef ModuleDef = {
PyModuleDef_HEAD_INIT,
"example",
"Example module",
-1,
ModuleMethods
};
PyObject* module = PyModule_Create(&ModuleDef);
return module;
}
For example, in the above example, the py_addNumbers
function is declared as static.
I saw in the official document that the native function is also declared as static.
Why do you want to do this, or can you not do it?