我的系统上安装了一个Python模块,我希望能够看到其中有哪些函数/类/方法可用。
我想对每一个都调用帮助函数。在Ruby中,我可以做一些类似ClassName的事情。方法获取该类上所有可用方法的列表。Python中有类似的东西吗?
例如:
from somemodule import foo
print(foo.methods) # or whatever is the correct method to call
我的系统上安装了一个Python模块,我希望能够看到其中有哪些函数/类/方法可用。
我想对每一个都调用帮助函数。在Ruby中,我可以做一些类似ClassName的事情。方法获取该类上所有可用方法的列表。Python中有类似的东西吗?
例如:
from somemodule import foo
print(foo.methods) # or whatever is the correct method to call
当前回答
r = globals()
sep = '\n'+100*'*'+'\n' # To make it clean to read.
for k in list(r.keys()):
try:
if str(type(r[k])).count('function'):
print(sep+k + ' : \n' + str(r[k].__doc__))
except Exception as e:
print(e)
输出:
******************************************************************************************
GetNumberOfWordsInTextFile :
Calcule et retourne le nombre de mots d'un fichier texte
:param path_: le chemin du fichier à analyser
:return: le nombre de mots du fichier
******************************************************************************************
write_in :
Ecrit les donnees (2nd arg) dans un fichier txt (path en 1st arg) en mode a,
:param path_: le path du fichier texte
:param data_: la liste des données à écrire ou un bloc texte directement
:return: None
******************************************************************************************
write_in_as_w :
Ecrit les donnees (2nd arg) dans un fichier txt (path en 1st arg) en mode w,
:param path_: le path du fichier texte
:param data_: la liste des données à écrire ou un bloc texte directement
:return: None
其他回答
如果你想获得当前文件中定义的所有函数的列表,你可以这样做:
# Get this script's name.
import os
script_name = os.path.basename(__file__).rstrip(".py")
# Import it from its path so that you can use it as a Python object.
import importlib.util
spec = importlib.util.spec_from_file_location(script_name, __file__)
x = importlib.util.module_from_spec(spec)
spec.loader.exec_module(x)
# List the functions defined in it.
from inspect import getmembers, isfunction
list_of_functions = getmembers(x, isfunction)
作为一个应用程序示例,我使用它来调用单元测试脚本中定义的所有函数。
这是一个密码组合,改编自Thomas Wouters和adrian的答案,以及Sebastian Rittau对另一个问题的回答。
对于你不想评估的代码,我推荐一种基于ast的方法(就像csl的答案),例如:
import ast
source = open(<filepath_to_parse>).read()
functions = [f.name for f in ast.parse(source).body
if isinstance(f, ast.FunctionDef)]
对于其他一切,inspect模块是正确的:
import inspect
import <module_to_inspect> as module
functions = inspect.getmembers(module, inspect.isfunction)
这给出了一个形式为[(<name:str>, <value:function>),…]的二元组列表。
上面的简单答案在各种回应和评论中都有暗示,但没有明确地指出来。
这样就可以了:
dir(module)
但是,如果您发现阅读返回的列表很烦人,只需使用下面的循环来每行获取一个名称。
for i in dir(module): print i
使用检查。Getmembers用于获取模块中的所有变量/类/函数等,并传入inspect。Isfunction作为谓词,只得到函数:
from inspect import getmembers, isfunction
from my_project import my_module
functions_list = getmembers(my_module, isfunction)
Getmembers返回一个元组列表(object_name, object),按名字的字母顺序排序。
你可以用inspect模块中的任何其他isXXX函数替换isfunction。
import types
import yourmodule
print([getattr(yourmodule, a) for a in dir(yourmodule)
if isinstance(getattr(yourmodule, a), types.FunctionType)])