Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing module level variables, from within a function in the module

Tags:

python

I'd like to be able to do something like this:

#mymodule var = None  def load():     var = something() 

Other module(s):

#secondmodule import mymodule mymodule.load()  #thirdmodule from mymodule import var print var #Shouldn't be None 

But I don't know how to reference a modules variable from a function defined in the module.

Is this possible? Or am I going to need to put a global declaration in every place I wan't to use this. Or am I going at this completely wrong?

like image 637
dennmat Avatar asked Aug 15 '11 00:08

dennmat


People also ask

Can modules access global variables?

A global variable is a variable which is accessible in multiple scopes. In Python, it is better to use a single module to hold all the global variables you want to use and whenever you want to use them, just import this module, and then you can modify that and it will be visible in other modules that do the same.

What is a module level variable?

Module-level variables can be either public or private. Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module. By default, variables declared with the Dim statement in the Declarations section are scoped as private.

Can functions be included within a module?

Functions provide the basic building blocks of functionality in larger programs (and computer simulations), and help to control the inherent complexity of the process. We can group functions together into a Python module (see modules), and in this way create our own libraries of functionality.


2 Answers

Just change the function definition to:

def load():     global var # this line has been added to the original code     var = something() 

Global variables are read-only from sibling methods. More accurately unless a variable is specified as global, Python consider it as local, but a read access to a local variable name will reach module-level scope if the name is not present in local scope.

See also use of “global” keyword in python and the doc for more details about the global statement

like image 87
Evpok Avatar answered Oct 13 '22 00:10

Evpok


You seem to mostly have it. You are missing only the fact that "module-level" variables are called global in Python. (They are not truly global, but only global to the module they are declared in, in other words.)

In any function where you modify a global variable (you want to make the name refer to a different object), it must be declared global. So your load() function needs a global var at the beginning. If you are only using the value of a global variable, or if it is a mutable type such as a list and you are modifying it, but not changing the object that the name points to, you needn't declare it global.

The import statement is, as you have discovered, how you can import a module-level variable from one module into another.

like image 30
kindall Avatar answered Oct 13 '22 01:10

kindall