symbol table is data structure that stores all information about identifier in source code of program.
1.local symbol table
symbol table in local scope. Access via locals() function . it's within a function, a class, or anything else.
2.global symol table
symbol table in global scope. Access via globals() function .All functions and variables that are not associated with any class or function are included in the global scope.
locals() and globals() return dictionary
example
$ python
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> a=1
>>>globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1}
>>> import math
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'math': <module 'math' from '/usr/lib/python3.10/lib-dynload/math.cpython-310-x86_64-linux-gnu.so'>}
using globals to display variable
>>>globals()['a']
Comments
Post a Comment