Python globals()

Python globals() is a powerful function for dynamically accessing and modifying global variables. It returns a dictionary of all global variables, allowing you to inspect or update them programmatically by understanding how globals() work; you can write more flexible and dynamic Python code.
Table of Contents

Understanding Python globals() Function

Python globals() is a built-in function that returns a dictionary representing the current global symbol table. This table is a dictionary containing information about the current module’s global variables. When you call globals(), it gives you a snapshot of all the global names (variables, functions, classes, etc.) currently defined and accessible from anywhere in your code. Each key in the dictionary is the name of a global variable, and its value is the value of that variable. Python globals() is mainly used for introspection or dynamically accessing and modifying global variables.

Syntax of Python globals()

global_namespace = globals()

Explanation

  • global_namespace: Variable will store the dictionary representing the global namespace.
  • globals(): Built-in function that returns the global symbol table as a dictionary.

Example of Python globals()

x = 10

global_dict = globals()
print(global_dict['x'])

Explanation

  • x = 10: Defines a global variable x and assigns it the value 10.
  • global_dict = globals(): Calls globals() and stores the resulting dictionary in global_dict.
  • print(global_dict['x']): Accesses the value of x from the global_dict dictionary using the key ‘x’ and prints it.

Output

10


globals() Parameters

Python globals() function does not take any parameters. It simply returns the dictionary of the current global symbol table when called. You don’t need to pass anything to globals() to get the global namespace.

Syntax

global_dictionary = globals()

Explanation

  • global_dictionary: Variable will store the dictionary returned by globals().
  • globals(): Function takes no parameters and returns the global namespace dictionary.

Example

def my_function():
    pass

global_vars = globals()
print(global_vars)

Explanation

  • def my_function():: Defines a function my_function.
  • pass: Placeholder in the function definition.
  • global_vars = globals(): Calls globals() and stores the resulting dictionary in global_vars.
  • print(global_vars): Prints the global_vars dictionary, which will include my_function and other global names.

Output

{…, ‘my_function’: <function my_function at 0x…>, …}


globals() Return Value

Python globals() function returns a dictionary representing the current global symbol table. This dictionary contains all the global variables, functions, classes, and objects defined in the current module’s scope. Each key in the dictionary is the name of a global object (as a string), and the corresponding value is the object itself. Modifying this dictionary directly can affect the global state of your program.

Example

y = 20
globals_dictionary = globals()
print(globals_dictionary["y"])

Explanation

  • y = 20: Defines a global variable y and assigns it the value 20.
  • globals_dictionary = globals(): Calls globals() and stores the returned dictionary in globals_dictionary.
  • print(globals_dictionary["y"]): Accesses the value of y from the globals_dictionary using the key “y” and prints it.

Output

20


Modify Global Variable using Globals in Python

You can use Python globals() to modify global variables, although modifying them directly is generally recommended. To change a global variable using globals(), you access the variable’s name as a key in the dictionary returned by globals() and assign a new value to it. This approach is useful when you need to update global variables dynamically, such as when the variable name is stored in a string.

Syntax

globals_dictionary = globals()
globals_dictionary[‘variable_name’] = new_value

Explanation

  • globals_dictionary: Variable stores the dictionary returned by globals().
  • globals(): Function returns the global symbol table dictionary.
  • globals_dictionary['variable_name']: Accesses the global variable named variable_name through the dictionary.
  • = new_value: Assigns a new value to the global variable.

Example

z = 5
print(f"Original value of z: {z}")
globals_dict = globals()
globals_dict['z'] = 15
print(f"Modified value of z: {z}")

Explanation

  • z = 5: Initializes a global variable z to 5.
  • print(f"Original value of z: {z}"): Prints the original value of z.
  • globals_dict = globals(): Gets the global namespace dictionary.
  • globals_dict['z'] = 15: Modifies the value of z through the globals_dict dictionary.
  • print(f"Modified value of z: {z}"): Prints the updated value of z.

Output

Original value of z: 5
Modified value of z: 15


Conclusion

Python globals() is a powerful function that inspects and interacts with the global namespace in Python. It returns a dictionary representing the global symbol table, allowing you to dynamically access and even modify global variables. While Python globals() can be useful for certain advanced programming techniques, accessing and modifying global variables directly by their names is generally a good practice for better code readability and maintainability. Understanding how globals() works can be valuable for debugging and gaining a deeper understanding of Python’s scoping rules.


Also Read

Python getattr()

Python exec()


Python Reference

python globals()

Table of Contents