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.
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.
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.