The globals() function returns the global symbol table as a dictionary. The function takes no arguments and returns a dictionary containing all references to the names in the global scope. By names we mean variable names, function names, imported module names e.tc

globals()

The function does not take any argument.

x = 100
y = 200
z = 300
def func():
    print(globals())

func()
import math
print(globals())

Use globals() to modify global variables

The globals() function can be used to access and modify global variables from within functions and classes. This is because the dictionary contains references to the actual variables.

my_global_var = "Hello World!" 
def modify_var():
    globals()['my_global_var'] = "Goodbye World!" 

modify_var()
print(my_global_var)

The globals() dictionary is also used to store information about imported modules. This allows modules to be imported only once, instead of having to import them every time they are accessed.

The globals() counterpart,  locals() function, is used similarly but in local scopes.