Python Global Variables

In this tutorial we will be discussing about Python global variables.

A variable which is declared outside of any function i.e. in the global space is known as global variable.

Global variables are useful when multiple functions need to use the same data. In C, C++ we can use and modify global variables directly in local functions and if we declare a local variable with the same name of global variable then it shadows the global variable but this is not the case in Python.

Let’s see this with the help of one example :

The output of the above program is:

5
5

Here x is global variable and it can be accessed in function and the program works correctly but what if we try to change the variable value inside the function.

When we run the above code, we will get the below error:

UnboundLocalError: local variable ‘x’ referenced before assignment

Here the problem is x in the function is now being treated as a local variable.

In python if it is just reading the variable and the variable doesn’t exist locally then it will try to look into any containing scope (Global scope in our first example). But this is not the case when we are trying to change the variable value. Python assumes that any variable whose value is being changed in any function is local to the function unless explicitly told.

To understand the above statement clearly let’s see one more example :

The output of the above code is:

5
2
5

If you are familiar with C or C++ you may expect:

5
2
2

As your output but in Python the x declared inside the function is treated as local variable and has no relation with the x declared outside the function.

To use the x as global variable in foo we should use global keyword, x inside the function will then be treated as a global variable.

Here is an example:

The output here is:

5
2
2

Using the global keyword we can make the function use the global copy of x and therefore any changes made to the function are reflected globally in x.

Why global keyword?

The global keyword is used because global variables are dangerous and are difficult to handle. Python wants to make sure that you really want to use global variable inside the function and therefore the global keyword is being used.

Let’s take an example to cover all the above points.

The output of the above program is:

5
6
5
7
7
7
6

Lets try to understand above code.

  • First call to hoo prints the global variable as it is just reading the variable x.
  • Calling boo will create a local copy of x inside the function and initialize it with 6 and therefore it will print 6. After the call to function is finished the local variable is discarded. This function will not affect global x.
  • As x is unaffected calling hoo again will print the global value of x i.e. 5
  • foo is using the global keyword and therefore it will now use the global copy of x and change made will directly change global x.
  • After the call to foo is executed value of global x is changed to 7 so print(x) will print 7.
  • hoo again will simply print the global value of x i.e. 7
  • Call to boo will create a local variable x with value 6 and will print it and global x will remain unchanged.

I hope now you got the idea about python global variables. Comment down below if you still have any queries.

1 thought on “Python Global Variables”

  1. Nice post, I didn’t know about the global keyword in Python, I actually always succeeded in avoiding the use of global variables. But it’s good to know they work this way.

Leave a Comment

Your email address will not be published. Required fields are marked *