如果您询问生成字节码时使用的符号表,请看一下symtable
模块。此外,Eli Bendersky撰写的这两篇文章非常有趣,而且非常详细:
在第2部分中,他详细介绍了一个可以打印出symtable描述的函数,但是它似乎是为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)