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


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer