89DEVs

Python: globals() function

globals() vs. locals()

The globals() function returns a dictionary containing all global variables, classes, and methods. To show the dictionary for local scope, use the locals() function.

globals() - show variables, classes and methods

To show the dictionary of all global varibles, classes and methods, call the globals() function. # globals() - show dictionary globals() All globals are returned. {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': }

globals() - modify globals

The globals() function can be used to modify values as well. # globals() - modify globals myInt = 5 globals()['myInt'] = 0 print(myInt) The value of myInt in the global scope is changed to integer 0. 0 To change the value on the local scope, use the locals() function.

globals() syntax

The syntax of the globals() function is: globals()

globals() arguments

The globals() function doesn't accept any arguments. If any argument is passed, a TypeError exception is raised.

globals() return values

The globals() function returns a dictionary with all global variables, classes, and methods.

related functions

The globals() function returns a dictionary of the global scope. To show a dictionary of the local scope, use the locals() function.

                
        

Summary


Click to jump to section