How can we see the Symbol-Table of a python source code?
I mean, Python makes a symbol table for each program before actually running it. So my question is how can I get that symbol-table as output?
Python is dynamic rather than static in nature. Rather than a symbol table as in compiled object code, the virtual machine has an addressible namespace for your variables.
The dir()
or dir(module)
function returns the effective namespace at that point in the code. It's mainly used in the interactive interpreter but can be used by code as well. It returns a list of strings, each of which is a variable with some value.
The globals()
function returns a dictionary of variable names to variable values, where the variable names are considered global in scope at that moment.
The locals()
function returns a dictionary of variable names to variable values, where the variable names are considered local in scope at that moment.
$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import base64
>>> dir(base64)
['EMPTYSTRING', 'MAXBINSIZE', 'MAXLINESIZE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_b32alphabet', '_b32rev', '_b32tab', '_translate', '_translation', '_x', 'b16decode', 'b16encode', 'b32decode', 'b32encode', 'b64decode', 'b64encode', 'binascii', 'decode', 'decodestring', 'encode', 'encodestring', 'k', 're', 'standard_b64decode', 'standard_b64encode', 'struct', 'test', 'test1', 'urlsafe_b64decode', 'urlsafe_b64encode', 'v']