Understanding the Global Keyword in Python

In this Python article, we will discuss every detail about the global keyword and how to use it in functions, nested functions, and modules with some examples.

Before we continue please go through the article on global, local and nonlocal python variables.

1. What is Global Keyword in Python?

In Python, we can declare variables in a global scope which means that the global variable can be accessed within the method and classes.

With global keyword we inform the Python interpreter to use the global variable and update it instead of creating a new variable with the local scope. In short, it is used to make changes to the global variable in a local context.

There are certain rules which have to be followed for the global variable.

1.1. Rules for using global keyword

  1. If a variable is not declared outside the function’s body, it will be treated as local.
  2. Referenced variables inside a function are implicitly global.
  3. Global keyword is mainly used for accessing a global variable inside a function for the purposes of updating that variable. If we just want to read the global variable then we do not need to use the global keyword.
  4. There is no need to use a global keyword outside a function.

1.2. Access global variable in a function

We can access a global variable inside a function without using the global keyword.

So let’s implement an example to check the usage of the global variable inside a funtion.

count = 5
def check():
    print('Running at', count) 

Running at 5

1.3. Update global variable in a function

If we want to update the value of a global variable inside a function that we need to use the global keyword.

Now let’s try to modify the value of global variable in the function without using the global keyword.

count = 5    
def change():
  count = count - 1 

Traceback (most recent call last):
  File "global.py", line 3, in change
    count = count - 1
UnboundLocalError: local variable 'count' referenced before assignment

We can clearly see that the value was not changed, it can only be changed with the help of global keyword.

count = 5    
def change():
  global count
  count = count - 1 


2. Using global Variables in Python Modules

In Python, we can create one module collect.py which can hold all the global variables and we can use it to share information across all the python modules within a program.

Let’s create one and learn how it works.

First, we need to create a collect.py file.

val_1 = 0
val_2 = "empty"
val_3 = 1

Create a growth.py file which can change global variables

import collect
collect.val_1 = 2
collect.val_2 = "alpha"

Now we need to create a main.py file which will check the changes in value

import collect
import growth

So, if we now run the main.py file, the output will be


For the above example, in total we have created three files: collect.pygrowth.py, and main.py.

The first module which we made collect.py file, will store all the global variables of val_1, val_2 and val_3. The second module growth.py file, will import the collect.py module and modify the values of val_1 and val_2.

At last, in the main.py file, we have imported both collect.py and growth.py  module and check for the changes made.

3. Global within Nested Functions

In Python, if we have a nested function and we need to modify the variable declared on the global level then we need to declare the global keyword in the inner function.

Rule: If we want to update the variable from global or nonlocal scope then we have to explicitly define it. If we just want to use the variables from other scopes then we don’t need an explicit statement and the compiler will automatically refer to the variable that is in the nearest scope.

value = "Global"

def outer():
  value = "Local"
  def change_val_global():
    global value
    value = value * 2
    print("Change global - ", value)


Change global -  GlobalGlobal

4. Conclusion

In this article, we have covered every detail that we need to know about the global keyword along with various suitable examples.

  • What is Global Keyword in Python?
  • Using Global Variables in Python Modules
  • How to use global within Nested Functions

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x