Symbol table: is a compiler-generated data structure used to store all the information needed to run a program.
Local symbol table: This symbol table stores all the information required for the local scope of a program and is accessed using the Python built-in locals () function
.
Syntax: locals ()
Parameters: This function does not take any input parameter.
Return Type: This returns the information stored in local symbol table.
Example # 1:
|
Exit :
Here no local variable is present: {} Here local variables are present: {’name’:’ Ankit’}
Example # 2: Updating using locals ()
.
Unlike globals (), this function cannot modify local symbol table data. The program below explains it clearly.
|
Exit:
Here no local variable is present: {} Here local variables are present: {’name’:’ Ankit’} Before updating name is: Ankit after updating name is: Ankit
Example # 3: locals ()
for the global environment .
The local symbol table is the same as the global symbol table for the global environment.
|
Exit:
This is using locals (): {’__file__’: ’/ home / 34dde64e1e47944021cdf478b97f13a0.py ’,’ __doc__ ’: None,’ __name__ ’:’ __main__ ’,’ __cached__ ’: None,’ __spec__ ’: None,’ __builtins__ ’: & lt-in module’ )ins’ (built , ’__package__’: None, ’__loader__’:% lt; _frozen_importlib_external.SourceFileLoader object at 0x7f88 5e463470"}
This is using globals (): {’__file__’: ’/home/34dde64e1e47944021cdf478b97f13a0.py’, ’__doc__’: None, ’__name__’: ’__main__’, ’__cached__’: None ’: None,’ __builtins__ ’: "module’ builtins’ (built-in) & gt ;, ’__package__’: None, ’__loader__’: "_frozen_importlib_external.SourceFileLoader object at 0x7f885e463470 & gtlock;}