Symbol Table in Python

2019-01-25 09:02发布

问题:

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?

回答1:

If you are asking about the symbol table that is used when generating bytecode, take a look at the symtable module. Also, these two articles by Eli Bendersky are fascinating, and very detailed:

Python Internals: Symbol tables, part 1

Python Internals: Symbol tables, part 2

In part 2, he details a function that can print out a description of a symtable, but it seems to have been written for Python 3. Here's a version for Python 2.x:

def describe_symtable(st, recursive=True, indent=0):
    def print_d(s, *args):
            prefix = ' ' *indent
            print prefix + s + ' ' + ' '.join(args)

    print_d('Symtable: type=%s, id=%s, name=%s' % (
            st.get_type(), st.get_id(), st.get_name()))
    print_d('  nested:', str(st.is_nested()))
    print_d('  has children:', str(st.has_children()))
    print_d('  identifiers:', str(list(st.get_identifiers())))

    if recursive:
            for child_st in st.get_children():
                    describe_symtable(child_st, recursive, indent + 5)


回答2:

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']


回答3:

Python does not make a symbol table before the program is executed. In fact, types and functions can be (and is normally) defined during the execution.

You may be interested in reading Why compile Python code?

Also see the detailed answer by @wberry



回答4:

You will likely enjoy Eli Bendersky's write up on the topic here

In CPython, you have the symtable module available to you.

In part 2, Eli describes a method which walks the symbol table that is incredibly helpful:

def describe_symtable(st, recursive=True, indent=0):
    def print_d(s, *args):
        prefix = ' ' * indent
        print(prefix + s, *args)

    assert isinstance(st, symtable.SymbolTable)
    print_d('Symtable: type=%s, id=%s, name=%s' % (
                st.get_type(), st.get_id(), st.get_name()))
    print_d('  nested:', st.is_nested())
    print_d('  has children:', st.has_children())
    print_d('  identifiers:', list(st.get_identifiers()))

    if recursive:
        for child_st in st.get_children():
            describe_symtable(child_st, recursive, indent + 5)