Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python globals() Function



ThePython 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 adictionary, where the names are the keys, and the associated values represent their current values or references.

Theglobals() is one of thebuilt-in functions and does not require any module.

Syntax

Syntax of the Pythonglobals() function is shown below −

globals()

Parameters

The Pythonglobals() function does not accept any parameters.

Return Value

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

globals() Function Examples

Practice the following examples to understand the use ofglobals() 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 = 22def 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 = 22def 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 auser-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 = 52numTwo = 121def 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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp