2014-10-28 41 views
6

在python中,您可以執行fname.__code__.co_names來檢索函數引用的函數和全局事物的列表。如果我做fname.__code__.co_varnames,我相信這包括內部函數。python - 列出函數的所有內部函數?

有沒有辦法做到inner.__code__.co_names?通過開始看起來像'inner'的字符串,由co_varnames返回?

+0

這是一個單一的事情,或者你想要嵌套函數? – 2014-10-28 13:51:52

回答

3

我不你認爲你可以檢查代碼對象,因爲內部函數是懶惰的,它們的代碼對象只有c按時完成。您可能想要看的是ast模塊。這裏有一個簡單的例子:

import ast, inspect 

# this is the test scenario 
def function1(): 
    f1_var1 = 42 
    def function2(): 
     f2_var1 = 42 
     f2_var2 = 42 
     def function3(): 
      f3_var1 = 42 

# derive source code for top-level function 
src = inspect.getsource(function1) 

# derive abstract syntax tree rooted at top-level function 
node = ast.parse(src) 

# next, ast's walk method takes all the difficulty out of tree-traversal for us 
for x in ast.walk(node): 
    # functions have names whereas variables have ids, 
    # nested-classes may all use different terminology 
    # you'll have to look at the various node-types to 
    # get this part exactly right 
    name_or_id = getattr(x,'name', getattr(x,'id',None)) 
    if name_or_id: 
     print name_or_id 

的結果是:功能1,功能2,f1_var1,功能3,f2_var1,f2_var2,f3_var1。強制免責聲明:可能沒有一個很好的理由做這種事情..但有樂趣:)

哦,如果你只想要的內部功能的名稱?

print dict([[x.name,x] for x in ast.walk(ast.parse(inspect.getsource(some_function))) if type(x).__name__=='FunctionDef']) 
+0

好的想法。如果從一個'pyc'運行,這會失敗,但我想這很不尋常。 – goncalopp 2014-10-28 15:31:57

+0

謝謝,這看起來像我會尋找。我這樣做的原因是因爲我試圖做功能跟蹤,並且我想在其中包含內部功能。 – user3475234 2014-10-28 16:44:21

3

在Python 3.4+中,您可以使用dis.get_instructions獲取名稱。爲了支持嵌套函數,以及你需要遞歸遍歷你遇到的每個代碼對象:

import dis 
import types 

def get_names(f): 
    ins = dis.get_instructions(f) 
    for x in ins: 
     try: 
      if x.opcode == 100 and '<locals>' in next(ins).argval\ 
               and next(ins).opcode == 132: 
       yield next(ins).argrepr 
       yield from get_names(x.argval) 
     except Exception: 
      pass 

演示:

def func(): 
    x = 1 
    y = 2 
    print ('foo') 
    class A: 
     def method(self): 
      pass 
    def f1(): 
     z = 3 
     print ('bar') 
     def f2(): 
      a = 4 
      def f3(): 
       b = [1, 2, 3] 
    def f4(): 
     pass 

print(list(get_names(func))) 

輸出:

['f1', 'f2', 'f3', 'f4']