在python中,您可以执行fname.__code__.co_names
来检索函数引用的函数和全局事物的列表。如果我做fname.__code__.co_varnames
,我相信这包括内部函数。python - 列出函数的所有内部函数?
有没有办法做到inner.__code__.co_names
?通过开始看起来像'inner'
的字符串,由co_varnames
返回?
在python中,您可以执行fname.__code__.co_names
来检索函数引用的函数和全局事物的列表。如果我做fname.__code__.co_varnames
,我相信这包括内部函数。python - 列出函数的所有内部函数?
有没有办法做到inner.__code__.co_names
?通过开始看起来像'inner'
的字符串,由co_varnames
返回?
我不你认为你可以检查代码对象,因为内部函数是懒惰的,它们的代码对象只有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'])
好的想法。如果从一个'pyc'运行,这会失败,但我想这很不寻常。 – goncalopp 2014-10-28 15:31:57
谢谢,这看起来像我会寻找。我这样做的原因是因为我试图做功能跟踪,并且我想在其中包含内部功能。 – user3475234 2014-10-28 16:44:21
在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']
这是一个单一的事情,或者你想要嵌套函数? – 2014-10-28 13:51:52