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

时间:2022-10-30 00:14:01

I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables) within a module?

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

I found this article about Python introspection, but I'm pretty sure it doesn't apply to Python 2.5. Thanks for the help.

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

4 个解决方案

#1


52  

Here are some things you can do at least:

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

import module

print dir(module) # Find functions of interest.

# For each function of interest:
help(module.interesting_function)
print module.interesting_function.func_defaults

#2


12  

Mark Pilgrim's chapter 4, which you mention, does actually apply just fine to Python 2.5 (and any other recent 2.* version, thanks to backwards compatibility). Mark doesn't mention help, but I see other answers do.

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

One key bit that nobody (including Mark;-) seems to have mentioned is inspect, an excellent module in Python's standard library that really helps with advanced introspection.

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

#3


6  

Just this is pretty good too:

这也很不错:

import module
help(module)

It will print the docstring for the module, then list the contents of the module, printing their docstrings too.

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

#4


4  

The dir() functions shows all members a module has.

dir()函数显示模块具有的所有成员。

#1


52  

Here are some things you can do at least:

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

import module

print dir(module) # Find functions of interest.

# For each function of interest:
help(module.interesting_function)
print module.interesting_function.func_defaults

#2


12  

Mark Pilgrim's chapter 4, which you mention, does actually apply just fine to Python 2.5 (and any other recent 2.* version, thanks to backwards compatibility). Mark doesn't mention help, but I see other answers do.

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

One key bit that nobody (including Mark;-) seems to have mentioned is inspect, an excellent module in Python's standard library that really helps with advanced introspection.

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

#3


6  

Just this is pretty good too:

这也很不错:

import module
help(module)

It will print the docstring for the module, then list the contents of the module, printing their docstrings too.

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

#4


4  

The dir() functions shows all members a module has.

dir()函数显示模块具有的所有成员。