Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python C API why is wrapper function static

So I have below sample code for extending python with C

#include <python.h>

static PyObject* sayhello(PyObject* self, PyObject *args) {
   const char* name;

   if (!PyArg_ParseTuple(arg, "s", &name))
       return NULL;

    printf("Hello %s !\n", name);

    Py_RETURN_NONE;
}

static PyMethodDef HelloMethods[] = 
{
    {"say_hello", say_hello, METH_VARARGS, "Greet Somebody."},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC inithello(void) {
    (void) Py_InitModule("hello", HelloMethods);
}

And My question is why below wrapper function static

   static PyObject* sayhello(PyObject* self, PyObject *args) {

1 Answers

A function in C can be kept static if it does not need to be referred to/linked by name outside its source file. In this case, the Python linkage is accomplished using references within the source file so no external links are needed. The code would work just as well if the function was externally visible but why pollute your name space?

like image 144
DrC Avatar answered May 31 '26 06:05

DrC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!