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 variablex
and assigns it the value 10.global_dict = globals()
: Callsglobals()
and stores the resulting dictionary inglobal_dict
.print(global_dict['x'])
: Accesses the value ofx
from theglobal_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 byglobals()
.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 functionmy_function
.pass
: Placeholder in the function definition.global_vars = globals()
: Callsglobals()
and stores the resulting dictionary inglobal_vars
.print(global_vars)
: Prints theglobal_vars
dictionary, which will includemy_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 variabley
and assigns it the value 20.globals_dictionary = globals()
: Callsglobals()
and stores the returned dictionary inglobals_dictionary
.print(globals_dictionary["y"])
: Accesses the value ofy
from theglobals_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 byglobals()
.globals()
: Function returns the global symbol table dictionary.globals_dictionary['variable_name']
: Accesses the global variable namedvariable_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 variablez
to 5.print(f"Original value of z: {z}")
: Prints the original value ofz
.globals_dict = globals()
: Gets the global namespace dictionary.globals_dict['z'] = 15
: Modifies the value ofz
through theglobals_dict
dictionary.print(f"Modified value of z: {z}")
: Prints the updated value ofz
.
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.