我正在開發一個插件,它是一種用於python代碼的類瀏覽器。我發現python模塊symtable有幫助。但是,這個模塊似乎有一個缺點。我無法使用此模塊獲取實例屬性。下面是在一個文件中的示例Python代碼名爲src.py:關於python模塊symtable
import os
class A():
cl_attr = 1
def fn1(self):
self.attr1 = 'hello'
def fn2(self, arg):
self.attr2 = arg
然後在Python解釋:
Python 2.7 (r27:82500, Jul 9 2010, 09:28:49)
[GCC 4.1.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> src = open('src.py', 'r').read()
>>> import symtable
>>> table = symtable.symtable(src, 'src.py', 'exec')
>>> table.get_children()[0].get_identifiers()
['fn1', 'cl_attr', 'fn2']
上述結果表明,類屬性cl_attr
返回。但是,當我嘗試:
>>> table.get_children()[0].get_children()[0].get_identifiers()
['self']
>>> table.get_children()[0].get_children()[1].get_identifiers()
['self', 'arg']
我只能獲取實例方法的參數,而實例屬性丟失。
因此,我想知道是否有辦法獲得實例屬性。如果不是,我不得不求助於另一個python模塊ast。任何人都可以幫助我嗎?謝謝!
謝謝!這是相當豐富的。但是我認爲對我來說,'ast'已經足夠了。無論如何,再次感謝! – user2346069 2013-05-06 05:02:36
@ user2346069:對於大多數實際使用情況,「ast」應該足夠了 – 2013-05-06 12:09:58