3
Contributors
2
Replies
27
Views
4 Years
Discussion Span
Last Post by slate
0

global variables are available everywhere after their definiton in a module (and outside the module can be qualified by the module name)

0

For clarification:
Global variable is defined by giving it a value at zero indentation level in the .py file.
For example in the following code globalvar is printed.

globvar=None

def some_function():
    print globalvar

There is a distinction if you want to change the value of the variable or just read it.

for example in the following code "something" is printed.

globalvar="something"

def somefunt():
    globalvar="something else"

somefunt()
print globalvar

To make the function modify the globalvar:

globalvar="something"

def somefunt():
    global globalvar
    globalvar="something else"

somefunt()
print globalvar

Note: The changing means to change to object that is labelled by the global variable.

For example

globalvar=[1]
def somefunt():
        globalvar.append(2)

somefunt()
print globalvar

#prints [1,2]
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.