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

▼ Show 1 Answer

0 votes
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()


The output is:

Learn More with Madanswer

Related questions

0 votes
asked Dec 22, 2019 in Python by rajeshsharma