简体   繁体   English

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

[英]List of all functions used in python program as string

How can we find all the functions in a python program??? 我们怎样才能找到python程序中的所有函数? for eg. 例如。

Input 输入

def func1:
  #doing something

def func2:
  #doing something

def func3:
  #doing something

Output 产量

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

If you want all functions in the global scope, you can use globals() with inspect.isfunction() : 如果您想要全局范围内的所有函数,可以将globals()inspect.isfunction()一起使用

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

Guessing you want only the methods in your current context: 猜猜你只想要当前环境中的方法:

import inspect

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM