如何在Python 2.5模块中列出方法?

Eva*_*ske 26 python introspection python-2.5

我正在尝试使用用C编写的Python库,它没有任何文档.我想使用内省来至少看看模块中有哪些方法和类.有人有一个函数或库我可以用来列出模块中的函数(带参数列表)和类(带方法和成员变量)吗?

我发现这篇关于Python内省的文章,但我很确定它不适用于Python 2.5.谢谢您的帮助.

Ale*_*erg 56

以下是您至少可以做的一些事情:

import module

print dir(module) # Find functions of interest.

# For each function of interest:
help(module.interesting_function)
print module.interesting_function.func_defaults
Run Code Online (Sandbox Code Playgroud)


Ale*_*lli 12

Mark Pilgrim的第4章,你提到它确实适用于Python 2.5(以及任何其他最新2.*版本,这要归功于向后兼容性).马克没有提到help,但我看到其他答案.

没有人(包括Mark ;-)似乎提到的一个关键点是inspect,这是Python标准库中的一个优秀模块,它真正有助于高级内省.


Ned*_*der 7

这也很不错:

import module
help(module)
Run Code Online (Sandbox Code Playgroud)

它将打印模块的docstring,然后列出模块的内容,也打印他们的文档字符串.