Global & Local Variables

Depending on where a variable is declared, the variable can be seen by other elements of the program code. There are two types of variable declarations: Global and Local.

Global variables are declared in the root area of the program code (generally at the beginning of your program code). The values stored in these variables will be accessible from any area of the code. This can be useful if you need to access a variable from anywhere in the program, but if you declare global variables when you don't need to access them all the time, they will sit and take up main memory, thus wasting precious computing resources. Use with care. Local variables are declared in a sub area of the program code, this may be in a function or procedure. The values stored in these variables will only be accessible from the sub program they are declared in. This can be friendly on computing resources as they are declared when the sub program is called, used, and when the sub program is exited, destroyed. So you only use main memory when you need to store the variable and it does not waste main memory like global variables do. However if you think you will need to variable outside the sub program you should think of using a global variable.

If you want to quickly tell the difference between a global and a local variable use these quick rules:

When programming in Python is is often better to use parameter passing so that you can provide the value of a variable to a sub-program within you main program. This can help to prevent errors in your programming.

Examples


def f(): 
    print s 
s = "I hate spam"
f()


The variable s is defined as the string "I hate spam", before we call the function f(). The only statement in f() is the "print s" statement. As there is no local s, the value from the global s will be used. So the output will be the string "I hate spam". The question is, what will happen, if we change the value of s inside of the function f()? Will it affect the global s as well? We test it in the following piece of code:


def f(): 
    s = "Me too."
    print s 

s = "I hate spam." 
f()
print s


The output looks like this:


Me too.
I hate spam.


What if we combine the first example with the second one, i.e. first access s and then assigning a value to it? It will throw an error, as we can see in the following example:


def f(): 
	print s
	s = "Me too."
	print s

s = "I hate spam." 
f()
print s


If we execute the previous script we get an error message:


UnboundLocalError: local variable 's' referenced before assignment



Python "assumes" that we want a local variable due to the assignment to s inside of f(), so the first print statement throws this error message. Any variable which is changed or created inside of a function is local, if it hasn't been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword "global", as can be seen in the following example:


def f():
    global s
    print s
    s = "That's clear."
    print s 

s = "Python is great!" 
f()
print s


Now there is no ambiguity. The output is as follows:


Python is great!
That's clear.
That's clear.



Local variables of functions can't be accessed from outside, when the function has finished:


def f():
    s = "I am globally not known"
    print s 

f()
print s


If you start this script, you get an output with the following error message:


I am globally not known
Traceback (most recent call last):
  File "global_local3.py", line 6, in 
    print s
NameError: name 's' is not defined



The following example shows a deliberate combination of local and global variables and function parameters:


def foo(x, y):
    global a
    a = 42
    x,y = y,x
    b = 33
    b = 17
    c = 100
    print a,b,x,y

a,b,x,y = 1,15,3,4
foo(17,4)
print a,b,x,y


The output of the script above looks like this:


42 17 4 17
42 15 3 4