globals()
globals() returns a dictionary of symbol table in global name space
globals() is same as using locals() in global name space
print(globals())
This will return a dictionary, some sample elements are shown here.
{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None ........
.........}
We can print any element by using key
my_var='plus2net'
print(globals()['my_var']) # plus2net
Default global variables
a=globals()
print(a['__name__'])
print(a['_dh'])
Using User defined function
x=5
def my_sum():
a=8
x=10 # local variable
print(globals()['x']) # 5
# print(globals()['a']) # KeyError
The last line will return KeyError as the variable a is not available in global name space.
«All Built in Functions in Python
locals()
dir() ord()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com