如果你不熟悉某個Python模塊,你可以通過以下方法獲取幫助信息:
- build-in的dir函數
- build-in的help函數
- 使用pydoc模塊
- 使用inspect模塊
dir函數
在Pyton中,任何事務都是對象,可以使用dir函數可以查看對象內所有的屬性和方法。
>>> import struct
>>> dir(struct)
['Struct', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from']
help函數
Python內置的幫助功能,它實際上是基於pydoc.help
>>> help()
Welcome to Python 2.7! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
help> cmp
Help on built-in function cmp in module __builtin__:
cmp(...)
cmp(x, y) -> integer
Return negative if x<y, zero if x==y, positive if x>y.
help> q
pydoc
開啓服務 python -m pydoc -p 1234
inspect
inspect函數可以在運行時幫助我們確定對象類型。對應的函數如下:
- ismodule()
- isclass()
- ismethod()
- isfunction()
- isgeneratorfunction()
- isgenerator()
- istraceback()
- isframe()
- iscode()
- isbuiltin()
- isroutine()
aceback()
- isframe()
- iscode()
- isbuiltin()
- isroutine()
…