Python globals() Function



The Python globals() function returns a dictionary representing the current global symbol table. It provides access to all the global variables and their corresponding values in the current scope. With the help of this feature, we can retrieve, modify, or delete global variables.

In Python, each program has a symbol table that contains information about the names (variables, functions, classes, etc.) defined in the program. The symbol table is represented by a dictionary, where the names are the keys, and the associated values represent their current values or references.

The globals() is one of the built-in functions and does not require any module.

Syntax

Syntax of the Python globals() function is shown below −

 globals() 

Parameters

The Python globals() function does not accept any parameters.

Return Value

The Python globals() function returns a dictionary representing the global symbol table.

globals() Function Examples

Practice the following examples to understand the use of globals() function in Python:

Example: Access Global Variable Using globals()

The following is an example of the Python globals() function. In this, we have created a method which is calling the globals() function to retrieve the global symbol table. By iterating over its items, we can access the global variable.

 nums = 22 def accessMethod(): globalVar = globals()["nums"] print(f"Accessing global number: {globalVar}") accessMethod() 

On executing the above program, the following output is generated −

 Accessing global number: 22 

Example: Modify Global Variable Using globals()

The globals() function not only allows us to retrieve global variables but also allows us to modify their values. In this example, the "modifyMethod()" function retrieves the global symbol table using globals() and modifies the value of "nums" by directly updating the dictionary entry.

 nums = 22 def modifyMethod(): globalVar = globals()["nums"] = 121 print(f"Modifying global number: {globalVar}") modifyMethod() 

The following is the output obtained by executing the above program −

 Modifying global number: 121 

Example: Access All Default Global Variables

In the following exmple, we are checking all the default global variables using globals() and then displaying them with the help of a user-defined function.

 def pritnGlobals(): for globalVars in globals().keys(): print(globalVars) pritnGlobals() 

The following output is obtained by executing the above program −

 __name__ __doc__ __package__ __loader__ __spec__ __annotations__ __builtins__ __file__ __cached__ pritnGlobals 

Example: Delete Global Attributes Using globals()

With the help of the globals() method, we can also delete a particular global attribute as illustrated in the below example.

 numOne = 52 numTwo = 121 def delGlobal(): del globals()["numTwo"] print(f"Is numTwo exist after deletion: {'numTwo' in globals()}") delGlobal() 

The above program, on executing, displays the following output −

 Is numTwo exist after deletion: False 
python_built_in_functions.htm
Advertisements