Python Global Namespace

The global namespace exists one level below the built-in namespace. Generally, it includes all non-nested names in the module (file) we are choosing to run the Python interpreter on. The global namespace is created when we run our main program and has a lifetime until the interpreter terminates (usually when our program is finished running). For example, take this program:

#Imaginary File: main.py import random first_name = "Jaya" last_name = "Bodegard" def print_variables(): random_number = random.randint(0,9) print(first_name) print(last_name) print(random_number)

Our script consists of a few different variables, a function, and an imported module. Right off the bat, it might not be clear what exists in the global namespace. Thankfully, in order to see what objects exist in the global namespace, Python provides the globals() built-in function.

Funnily enough, the globals() function actually comes from the built-in namespace (and is thus called a built-in function), and we can access it anywhere in our program (or any program)! Let’s see what it would return if we print the output inside of the program above:

# ...code from above (omitted for brevity) print(globals())

Would output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f224a7ae4c0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'main.py', '__cached__': None, 'random': <module 'random' from '/usr/lib/python3.8/random.py'>, 'first_name': 'Jaya', 'last_name': 'Bodegard', 'print_variables': <function print_variables at 0x7f224a76a1f0>}

Again, we have a lot of nonsense-looking text. Don’t worry there are only two things we should focus on:

  1. The global namespace contains all of the non-nested objects of our program. This includes the variables first_name and last_name as well as the function print_variables. However, the random_number variable is not included in the namespace because it is nested inside of our function. Don’t worry we will learn about the namespace that random_number exists inside in the next exercise.

  2. Anytime we use the import statement to bring in a new module into our program, instead of adding every name from that module (such as all the names in the random module) to our current global namespace, Python will create a new namespace for it. This means there might be potentially multiple global namespaces in a single program. This will be masked away from us in the format seen with the random module (<module 'random' from '/usr/lib/python3.8/random.py'>).

Don’t fret over the rest of the namespace we didn’t touch on, it is a lot of behind-the-scenes built-in python objects that are usually present in all scripts. For now, we will focus on the parts we can manipulate.

Let’s practice using the globals() function to check out the global namespace in a program!



Note: Since we will be printing a lot of namespaces, the current editor has markers for where to write each checkpoint.

Let’s start by examining the global namespace when the script is empty.

Call globals() inside of a print() function to observe the current global namespace.


Let’s now add some Python code to see how the namespace changes. Add a variable called global_variable with the value of 'global'.


Let’s add some more code to populate the namespace! Add a function called print_global() that does the following:

  • Defines a variable inside called global_variable with the value of 'nested global'.

  • Defines a variable inside called nested_variable with the value of 'nested value'.


Let’s examine how the global namespace has changed. Print the globals() function once more.


Here are some important takeaways to note from the comparison:

  • As we saw previously, the global namespace only contains items that are non-nested. In this case, our global namespace does not contain the identical nested name of global_variable.

  • Depending on where we call globals() we will have a different namespace generated. This means globals() will show the namespace at the time it was executed. Since we called globals() a second time after defining a few items (such as variables and functions), those items now show up in the global namespace.

Play around with the code and run it one more time to move onto the next exercise.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?