Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why function is static in python?

Tags:

c++

c

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?

like image 417
Siva Krishna Aleti Avatar asked Sep 11 '13 15:09

Siva Krishna Aleti


People also ask

Why are functions static?

The “static” keyword before a function name makes it static. For example, below function fun() is static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static.

What does it mean when a function is static Python?

Static methods in Python are extremely similar to python class level methods, the difference being that a static method is bound to a class rather than the objects for that class. This means that a static method can be called without an object for that class.

Why static variable is used in Python?

When we declare a variable inside a class but outside any method, it is called as class or static variable in python. Class or static variable can be referred through a class but not directly through an instance.

Why would a method be declared static?

In Java programming, the main motivation for making a method static is convenience. You can call a static method without creating any object, just by using its class name. So if you need a method, which you want to call directly by class name, make that method static.


1 Answers

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.

like image 109
m01 Avatar answered Sep 29 '22 14:09

m01