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

1 Answer

0 votes
by
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

Related questions

0 votes
asked Dec 22, 2019 in Python by rajeshsharma
0 votes
asked Feb 11, 2020 in Python by rahuljain1
...