2012-01-31 23 views
8

我們如何看到Python源代碼的符號表?Python中的符號表

我的意思是,在每個程序實際運行之前,Python爲它製作了一個符號表,所以我的問題是如何將該符號表作爲輸出?

+4

什麼讓你覺得蟒蛇使一個符號表? – 2012-01-31 19:17:24

+2

@WinstonEwert :: http://docs.python.org/library/symtable.html – 2012-02-05 21:27:43

回答

6

如果你問有關生成的字節碼時使用符號表,看看在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) 
5

在程序執行前,Python不會生成符號表。實際上,類型和函數可以在執行期間(並且通常)被定義。

你可能有興趣在閱讀Why compile Python code?

而且通過@wberry

7

的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'] 
+1

那麼[symtable](http://docs.python.org/library/symtable.html)是做什麼的? – voithos 2012-01-31 19:22:32

+0

你忘了'locals()' – Marcin 2012-01-31 19:23:08

+0

@voithos他特別詢問了Python源代碼。我沒有使用該模塊,但我相信它是用於訪問虛擬機內部或C擴展內部,這是不一樣的事情。 – wberry 2012-01-31 19:24:57

2

你可能會享受禮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) 
+0

請注意,'describe_symtable'似乎是爲Python3編寫的。 – voithos 2012-01-31 19:46:07