Python globals() Function with Examples

In the previous article, we have discussed Python filter() Function with Examples
globals() Function in Python:

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

A symbol table contains information about the current program that is required.

Variable names, methods, classes, and so on are examples of this.

There are two types of symbol tables.

  1. Local symbol Table
  2. Global symbol Table

The local symbol table stores all information related to the program’s local scope and is accessed in Python via the locals() method.

The local scope could be within a function, a class, or something else.

Similarly, a Global symbol table stores all information related to the program’s global scope and is accessed in Python via the globals() method.

All functions and variables that are not associated with any class or function are included in the global scope.

Syntax:

globals()

Parameter Values: The globals() method does not accept any parameters.

Return Value:

This method returns the dictionary containing the current global symbol table.

globals() Function with Examples in Python

1)Using global variables, make changes to global variables ()

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number(id) as static input and store it in a variable.
  • Modify the given number to some random number by using the globals() function.
  • Print the new number after modification.
  • The Exit of the Program.

Below is the implementation:

# Give the number(id) as static input and store it in a variable.
gvn_id = 10
# Modify the given number to some random number by using the globals() function.
globals()['gvn_id'] = 5
# Print the new number after modification.
print('The modified Id is:', gvn_id)

Output:

The modified Id is: 5
How does Python’s globals() method work?

Below is the implementation:

# Take a variable and initialize it with the globals() function.
m = globals()
# Print the above result.
print(m)

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f0e651f0cc0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/78f96300568d7239be1e5414d328ce86.py', '__cached__': None, 'm': {...}}

All global variables and other symbols for the current program are displayed in the output.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number(id) as user input using the int(input()) function and store it in a variable.
  • Give the new number as user input using the int(input()) function and store it in another variable.
  • Modify the given number to the above given new number by using the globals() function.
  • Print the new number after modification.
  • The Exit of the Program.

Below is the implementation:

# Give the number(id) as user input using the int(input()) function and store it in a variable.
gvn_id = int(input("Enter some random number = "))
# Give the new number as user input using the int(input()) function and store it in another variable.
new_id = int(input("Enter some random number = "))
# Modify the given number to the above given new number by using the globals() function.
globals()['gvn_id'] = new_id
# Print the new number after modification.
print('The modified Id is:', gvn_id)

Output:

Enter some random number = 20
Enter some random number = 50
The modified Id is: 50

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.