Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbol Table in Python

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?

like image 858
Rahul Gupta Avatar asked Jan 31 '12 19:01

Rahul Gupta


1 Answers

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']
like image 164
wberry Avatar answered Sep 18 '22 20:09

wberry