如何列出一个模块中的所有函数?

21 浏览
0 Comments

如何列出一个模块中的所有函数?

我在系统中安装了一个Python模块,我想查看其中可用的函数/类/方法。

我想在每个函数上调用help函数。在Ruby中,我可以像ClassName.methods这样做,以获取该类上可用的所有方法列表。在Python中是否有类似的功能?

例如,像这样的东西:

from somemodule import foo
print(foo.methods)  # or whatever is the correct method to call

admin 更改状态以发布 2023年5月21日
0
0 Comments

使用inspect模块:\n

from inspect import getmembers, isfunction
from somemodule import foo
print(getmembers(foo, isfunction))

\n此外,还可以看到pydoc模块,交互式解释器中的help()函数以及pydoc命令行工具,它们都可以生成你需要的文档。你只需要给它们想要查看文档的类即可。它们还可以生成HTML等输出并将其写入磁盘。

0
0 Comments

\n\n你可以使用 dir(module) 来查看所有可用的方法/属性。同时还可以查看 PyDocs。

0