Why is function static in python?

If we want to integrate C / C ++ into python using the Python internal API. Then the signature of the functions has the following forms

static PyObject *MyFunction( PyObject *self, PyObject *args ); static PyObject *MyFunctionWithKeywords(PyObject *self, PyObject *args, PyObject *kw); static PyObject *MyFunctionWithNoArgs( PyObject *self ); 

Why are these functions implemented as static?

+7
c ++ c
source share
4 answers

From docs :

Consequently, portability does not require any assumptions about the visibility of characters. This means that all characters in extension modules must be declared static , except for the initialization of function modules, in order to avoid name conflicts with other extension modules (as described in the Table of Methods and Initialization of Function Modules). And that means that characters that should be accessible from other extension modules should be exported differently.

+5
source share

So you ask what static means.

This means that these functions are only available in the file in which they are declared, so as not to contradict other definitions and the namespace of contaminants.

The reason these files are static is because all python functions will display as they cover all permutations of declarations of possible functions. They can only be created in this file.

+2
source share

Therefore, they are available only in the file in which they are defined. In order not to pollute the global namespace.

0
source share

The static before a function in C means that this function is not visible outside the translation unit (roughly speaking, the source file after the header files was included) in which it is defined. It gives an "internal link" (in C language) to the function, so that it is "private" to the file.

0
source share

All Articles