python程序中用作字符串的所有函数的列表

时间:2012-01-17 18:40:38

标签: python string function

我们怎样才能找到python程序中的所有函数?例如。

输入

def func1:
  #doing something

def func2:
  #doing something

def func3:
  #doing something

输出

{'func1' , 'func2' , 'func3'}

2 个答案:

答案 0 :(得分:1)

如果您想要全局范围内的所有功能,可以将globals()inspect.isfunction()一起使用:

>>> def foo():
...     pass
... 
>>> def bar():
...     pass
... 
>>> import inspect
>>> [member.__name__ for member in globals().values() \
...                  if inspect.isfunction(member)]
['bar', 'foo']

答案 1 :(得分:1)

猜测你只想要当前环境中的方法:

import inspect

d = locals()
funcs = [f for f in d if inspect.isfunction(d[f])]