Symbol Table in Python

断了今生、忘了曾经 提交于 2019-11-29 03:57:57

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)

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

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!