Python globals() Function – Tutorial with Examples

The globals() function in Python is a built-in function that returns a dictionary representing the current global symbol table. This symbol table contains all the global variables and functions defined in the current scope. The globals() function is useful when you want to access or manipulate the global variables in your code.

Syntax

globals()

Parameters

The globals() function does not take any parameters.

Return Value

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

Examples

Example 1: Accessing Global Variables

x = 10
y = 20
z = 30
print(globals())

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f9e624a8c18>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'x': 10, 'y': 20, 'z': 30}

In this example, the globals() function is used to access the global variables defined in the current scope. The function returns a dictionary that includes all the global variables, as well as other system-defined variables and functions. In this case, the dictionary includes the variables x, y, and z.

Example 2: Modifying Global Variables

x = 10
y = 20
z = 30
globals()["x"] = 40
print(x)

Output:

40

In this example, the globals() function is used to modify a global variable. The globals() function is called to access the global symbol table, and then the value of the x variable is updated using the square bracket notation. The modified value of x is then printed, which shows that the change was successful.

Example 3: Adding a Global Variable

x = 10
y = 20
z = 30
globals()["w"] = 50
print(w)

Output:

50

In this example, the globals() function is used to add a new global variable. The globals() function is called to access the global symbol table, and then a new variable named w is added using the square bracket notation. The value of w is then printed, which shows that the new variable has been successfully added to the global symbol table.

Use Cases

The globals() function is useful in a number of different situations, including:

  • Accessing global variables for read or write operations
  • Adding new global variables to the symbol table
  • Modifying the values of existing global variables
  • Accessing system-defined variables and functions in the global symbol table

It is important to note that using the globals() function can make your code less readable, as it makes it harder to keep track of which variables are global and which are local. It is generally recommended to use the global keyword instead of the globals() function to modify global variables in your code. This makes your code more readable and easier to maintain.

In conclusion, the globals() function is a useful tool for accessing and manipulating the global symbol table in Python, but it should be used with caution in order to maintain the readability and maintainability of your code.

Leave a Reply

Your email address will not be published. Required fields are marked *