Categories

Dec 22, 2019 in Python

Q: What Is The Use Of Globals() Function In Python?

1 Answer

Dec 22, 2019
The globals() function in Python returns the current global symbol table as a dictionary object.

Python maintains a symbol table to keep all necessary information about a program. This info includes the names of variables, methods, and classes used by the program.

All the information in this table remains in the global scope of the program and Python allows us to retrieve it using the globals() method.

Signature: globals()

Arguments: None

# Example: globals() function

x = 9

def fn():

    y = 3

    z = y + x

    # Calling the globals() method

    z = globals()['x'] = z

    return z

       

# Test Code     

ret = fn()

print(ret)

The output is:

12
Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Aug 29 in Python
Dec 14, 2019 in Python
Dec 14, 2019 in Python
...