Python中的符号表

Symbol Table in Python

我们如何看到python源代码的符号表?

我的意思是,Python在实际运行每个程序之前都会为其创建一个符号表。 所以我的问题是如何获取该符号表作为输出?


Python本质上是动态的,而不是静态的。虚拟机具有变量的可寻址命名空间,而不是已编译目标代码中的符号表。

dir()dir(module)函数返回代码中该点的有效名称空间。它主要用于交互式解释器中,但也可以由代码使用。它返回一个字符串列表,每个字符串都是具有某些值的变量。

globals()函数将变量名的字典返回到变量值,此时变量名在范围内被认为是全局的。

locals()函数将变量名的字典返回到变量值,此时变量名被认为是作用域内的局部变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ 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']


如果您询问生成字节码时使用的符号表,请查看symtable模块。另外,Eli Bendersky撰写的这两篇文章非常有趣,而且非常详细:

Python内部构件:符号表,第1部分

Python内部构件:符号表,第2部分

在第2部分中,他详细介绍了一个可以打印出symtable描述的函数,但它似乎是为Python 3编写的。这是Python 2.x的一个版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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不会创建符号表。实际上,可以在执行过程中(通常是)定义类型和函数。

您可能对阅读为什么编译Python代码感兴趣?

另请参阅@wberry的详细答案


您可能会喜欢Eli Bendersky在此处撰写的有关该主题的文章

在CPython中,您可以使用symtable模块。

在第2部分中,Eli描述了一种遍历符号表的方法,该方法非常有用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)