回答
如果你問有關生成的字節碼時使用符號表,看看在symtable
模塊。此外,由Eli Bendersky這兩篇文章是迷人的,並很詳細:
Python Internals: Symbol tables, part 1
Python Internals: Symbol tables, part 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)
的Python看到詳細的答案是動態的而不是靜態的性質。虛擬機不像編譯目標代碼那樣使用符號表,而是爲變量提供可尋址的名稱空間。
dir()
或dir(module)
函數返回代碼中該點的有效名稱空間。它主要用於交互式解釋器,但也可以用於代碼。它返回一個字符串列表,每個字符串都是具有某個值的變量。
globals()
函數將變量名稱的字典返回給變量值,其中變量名稱在該時刻被認爲是全局的。
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']
你可能會享受禮Bendersky的寫了關於這個專題here
在CPython中,您可以使用symtable
模塊。
在part 2,禮描述了行走的符號表是難以置信的幫助的方法:
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)
請注意,'describe_symtable'似乎是爲Python3編寫的。 – voithos 2012-01-31 19:46:07
- 1. 符號在Python中
- 2. Python中@符號的用途?
- 3. python中的變音符號
- 4. Python中的奇怪符號
- 5. Python的正則表達式來找到符號數字符號
- 6. 重複標點符號和符號的Python正則表達式
- 7. python中的符號字符的含義
- 8. Python字符串/列表符號£符號寫入文件失敗?
- 9. Python的硒send_keys表情符號支持
- 10. Python - 上標字符串中的符號
- 11. Python中的符號vs運算符
- 12. 如何在字符串(python,perl)中查找表情符號,unicode字符和表情符號的文本描述?
- 13. Python中不更換%符號
- 14. 從python中的列表中的符號解析單詞
- 15. 從Python中的字符串列表中刪除章節號
- 16. 通過Python瞭解符號表
- 17. 在Python中重新解釋帶符號的無符號長度
- 18. 鏈接中的符號表
- 19. Verilog中的表達符號
- 20. Java中的符號表
- 21. NSTextView中的表情符號
- 22. 渲染中的表情符號:表情符號:格式
- 23. 使用逗號/列表的Python切片符號
- 24. Python字符串符號
- 25. 將表情符號/表情符號添加到MSSQL表中
- 26. 符號數學Python?
- 27. Python的複數符號
- 28. Python的無符號右移
- 29. Python的逃亡「{}」符號
- 30. Python的符號或操作 「**」
什麼讓你覺得蟒蛇使一個符號表? – 2012-01-31 19:17:24
@WinstonEwert :: http://docs.python.org/library/symtable.html – 2012-02-05 21:27:43