Global Variables in Python

In the previous article we looked at how we can return multiple variables in Python. Now let us discuss about Global Variables in Python, Global variables are the variables declared outside of functions and classes and that are accessible throughout the entire program. Here in this article we will mainly look at the use of Global Variables in Python with respect to functions. Let us first look at an example:

def fun( ):
#Local variables(Accessible only within the functions or classes)
    a=10
    b=20
    print(a,b,c,d)

#Global Variables(Accessible through the entire program)
c=30
d=40
print(c,d)

Since a and b are created within the function they are local variables of the fun( ) function. We can access the global variables as well from within the function(c,d). However, trying to access the local variables outside of the area from where it was declared would yield an error. What would be the output of the below program?

def fun( ):
    #Local variable x
    x=10

#Global variable x
x=15
fun( )
print(x)
Output:
15

The value of the x which gets printed is the one defined and accessible in the main body of the program, hence we obtain the output result of 15. What if we wanted to change the global variable inside the function? Or we wanted the program to print the other x variable? There is a special keyword for that purpose ‘global’. Let us demonstrate, it’s usage:

def fun( ):
    #Referring to the global variable x through the use of global() function
    global x
    #Local variable x
    x=10

x=15
fun( )
print(x)
Output:
10

The program now prints the other x, now the global keyword converts the local variable into a globalized one and any change or modification to the local x variable is going to be reflected through the entire program.

Look at the below program and try to guess the output:

def fun( ):
    y=x+5
    print(y)
x=15
fun( )

In this program we have the global variable x, within the function we are adding the value to x as the initialization for the value of y.

Output:
20

However if the program was written in this manner instead:

def fun( ):
x=x+5
print(x)
x=15
fun( )

We obtain an error similar to the one below:

Output:
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    fun( )
  File "main.py", line 2, in fun
    x=x+5
UnboundLocalError: local variable 'x' referenced before assignment

The program fails to execute as it assumes the x being referenced refers to the global variable, it would work without issues if it was written as x=5 as that would simply create a new local variable but of the same name.

Let us look at another program, in this program we have a global variable with the value of 15 and inside the function we have a local variable with the same name, when we have a local variable with the same name it hides the variable. How do we access the global variable of the same name from within the same area(function) as where the local variable is declared? We can do so through the use of the globals( ) function, we can access a list of every global variable or reference a particular global variable(specified within square brackets) through the use of this function.

def fun( ):
    x=10
    #Obtaining the global variable x value through the use of globals() function
    globals( )['x']=20
    print(x)
x=15
fun( )
print(x)
Output:
10
20

Hence, we have explored the concept of Global variable in Python in the next article we will see how we can write a program to return the first digit of a number in Python and what is the logic behind such a program’s working.

Leave a Comment