Python中的符号表

Python中的符号表,python,symbol-table,Python,Symbol Table,如何查看python源代码的符号表 我的意思是,Python在实际运行程序之前为每个程序创建一个符号表。因此,我的问题是如何获得该符号表作为输出?在程序执行之前,Python不会生成符号表。事实上,类型和函数可以(并且通常是)在执行期间定义 你可能对阅读感兴趣 另请参见@wberry的详细回答,Python本质上是动态的,而不是静态的。虚拟机为变量提供了一个可寻址的命名空间,而不是编译的对象代码中的符号表 dir()或dir(module)函数返回代码中该点的有效命名空间。它主要用于交互式解释器

如何查看python源代码的符号表


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

在程序执行之前,Python不会生成符号表。事实上,类型和函数可以(并且通常是)在执行期间定义

你可能对阅读感兴趣


另请参见@wberry的详细回答,Python本质上是动态的,而不是静态的。虚拟机为变量提供了一个可寻址的命名空间,而不是编译的对象代码中的符号表

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

globals()
函数将变量名字典返回给变量值,此时变量名的作用域是全局的

$ 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']
locals()
函数将变量名字典返回给变量值,此时变量名在作用域中被认为是局部的

$ 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']
$python
Python 2.6.5(r265:79063,2010年4月16日,13:57:41)
[GCC 4.4.3]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>本地人()
{'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>>globals()
{'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>>目录()
[“内置”、“文件”、“名称”、“包装”]
>>>导入base64
>>>总监(base64)
['EMPTYSTRING'、'MAXBINSIZE'、'MAXLINESIZE'、'uuuu all'、'uuuuu builtins'、'uuuuuuu doc'、'uuuuuu file'、'uuuuuuuuu name'、'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu包'、'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,'standard_b64decode','standard_b64encode','struct','test','test1','urlsafe_b64decode','urlsafe_b64encode','v']

如果您询问生成字节码时使用的符号表,请查看模块。此外,Eli Bendersky的这两篇文章很吸引人,而且非常详细:

在第2部分中,他详细介绍了一个可以打印符号表描述的函数,但它似乎是为Python 3编写的。下面是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)

你可能会喜欢Eli Bendersky关于这个话题的评论

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

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

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)

是什么让你认为python可以生成一个符号表?@WinstonEwert::python在不同名称空间级别维护的符号表的相关帖子-@voithos他特别询问了python的源代码。我没有用过这个模块,但我相信它是用来访问VM内部或C扩展内部的,这不是一回事。@voithos,但也许他真的是指VM内部。我想你应该把它作为一个答案发布出来。注意,
descripe\u symtable
似乎是为Python3编写的。