8

If we want to integrate C/C++ into python using internal Python API. Then signature of functions are in 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 these functions are implemented as static?

4 Answers 4

6

From the docs:

Portability therefore requires not to make any assumptions about symbol visibility. This means that all symbols in extension modules should be declared static, except for the module’s initialization function, in order to avoid name clashes with other extension modules (as discussed in section The Module’s Method Table and Initialization Function). And it means that symbols that should be accessible from other extension modules must be exported in a different way.

Sign up to request clarification or add additional context in comments.

Comments

2

So you are asking what static means.

This mean that these functions are only available in the file in which they are declared so as to not conflict with other definitions and pollute namespace.

The reason that these files are static are because, these are which all python functions will be mapped see how they cover all the permutations of possible functions declarations. These can only be created here in that file.

1 Comment

This doesn't answer the op's question, it answers a different question. From the op's question he seems to know what static is, just not why these specific functions are static.
0

So they are only available in the file in which they are defined. To not pollute the global name space.

2 Comments

They do "pollute the global namespace", but they have internal linkage, so they don't incur ODR violations.
They pollute the global namespace in the current file, but they don't pollute the "global linker namespace", if we want to call it like that.
0

The keyword static before a function in C means that that function is not visible outside the translation unit (roughly, the source file after the header files have been included) in which it is defined. It gives "internal linkage" (in C parlance) to the function, so that it is "private" to the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.