Global Variables in Python With Examples

In this tutorial, We will look into Python Global Variables. We will learn how to define a global variable and then how to access them in a function.

Global Variables in Python

Global Variables are defined outside a function. We can access a global variable in all parts of the Python program.

Python Global Variables Example

Let’s look at a simple example to declare a global variable. Then we will use this global variable in a function.

Output:

The variable “website” is defined in the program, outside of a function. So, it becomes a global variable.

Using global keyword to access global variable

If there is a variable defined inside a function with the same name, then we have to use global keyword to access the global variable. Otherwise, the local variable value will be used.

Let’s look at a quick example to use the global keyword to access global variables.

Output:

If we don’t use the global keyword, the program will throw error message “UnboundLocalError: local variable referenced before assignment”.

The best practice is to avoid using the same name as the global variable and stay away from the name conflicts.

Quick word on Python globals() function

Python globals() function return a dictionary representing the current global symbol table. This dictionary contains the global variables name and its values. We can use this function to get quick information on all the global variables.

Python program maintains program information in symbol tables.

There are two types of symbol tables:

  1. Local Symbol Table – stores information related to the local scope of the program.
  2. Global Symbol Table – stores information related to global scope of the program.

Python symbol table contains details about variable names, methods, classes, etc.

Python globals() function doesn’t accept any arguments.

Output:

It also prints the python script from where this function is called.

Let’s see the output if the same function is called from the Python interpreter.

This time there is no __file__ attribute in the output.

Global Variables in globals() dictionary

As we mentioned earlier that the global symbol table contains information about the global variables.

Let’s see this with a simple example.

Output:

The global symbol dictionary contains the ‘name’ variable too.

One of the features of globals() is that we can modify the variables in the global dictionary. Since it’s a dictionary, we can get the value of a specific key too.

Output: David

Let’s see if globals() contains local scope variables or not. For this, let’s define a function and some variables inside it.

Output:

The ‘gl’ variable is part of the global dictionary because its scope is global. Whereas, ‘local’ variable is not part of the global dictionary because of having local scope.

Conclusion

Python global variables are very easy to define and use. The globals() function returns the global variables and it can be used in debugging the code. We can use it to identify the global scope variables and functions available to the program. We can also modify the global variable data, but that is not advisable and mostly not required in normal programs.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply