我的系统上安装了一个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
当前回答
这样就可以了:
dir(module)
但是,如果您发现阅读返回的列表很烦人,只需使用下面的循环来每行获取一个名称。
for i in dir(module): print i
其他回答
除了之前的回答中提到的dir(模块)或help(模块),还可以尝试: -打开ipython - import module_name - type module_name,按tab键。它会打开一个小窗口,列出python模块中的所有函数。 看起来很整洁。
下面是hashlib模块的所有函数的代码片段
(C:\Program Files\Anaconda2) C:\Users\lenovo>ipython
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import hashlib
In [2]: hashlib.
hashlib.algorithms hashlib.new hashlib.sha256
hashlib.algorithms_available hashlib.pbkdf2_hmac hashlib.sha384
hashlib.algorithms_guaranteed hashlib.sha1 hashlib.sha512
hashlib.md5 hashlib.sha224
如果你不能在没有导入错误的情况下导入上述Python文件,这些答案都不会起作用。当我检查一个来自大量依赖的大型代码库的文件时,我就遇到了这种情况。下面的代码将把文件作为文本处理,搜索所有以“def”开头的方法名,并打印它们及其行号。
import re
pattern = re.compile("def (.*)\(")
for i, line in enumerate(open('Example.py')):
for match in re.finditer(pattern, line):
print '%s: %s' % (i+1, match.groups()[0])
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
这将把your_module中定义的所有函数添加到列表中。
result=[]
for i in dir(your_module):
if type(getattr(your_module, i)).__name__ == "function":
result.append(getattr(your_module, i))
使用inspect模块:
from inspect import getmembers, isfunction
from somemodule import foo
print(getmembers(foo, isfunction))
还可以查看pydoc模块、交互式解释器中的help()函数和生成所需文档的pydoc命令行工具。您可以只给他们您希望看到的类的文档。例如,它们还可以生成HTML输出并将其写入磁盘。