WEB开发网
开发学院软件开发Python 可爱的 Python: pydoc 和 distutils 模块 阅读

可爱的 Python: pydoc 和 distutils 模块

 2008-11-13 13:18:16 来源:WEB开发网   
核心提示: 清单 3:检查类的继承结构% cat mymod2.pyfrom mymod import MyClassclass MyClass2(MyClass):"""Child class"""def foo(self):pass% p

清单 3:检查类的继承结构

% cat mymod2.py
from mymod import MyClass
class MyClass2(MyClass):
  """Child class"""
  def foo(self):
    pass
% pydoc.py mymod2.MyClass2
Python Library Documentation: class MyClass2 in mymod2
class MyClass2(mymod.MyClass)
| Child class
|
| __init__(self, spam=1, eggs=2) from mymod.MyClass
|
| foo(self)

在这个快速报告中,我们可以知道 MyClass2 有 __init__() 和 foo() 方法(以及相应的参数),哪个方法是类自身实现的以及其它哪些方法是继承而来(以及被继承的类所处的位置)。

另一个美妙的类似于 manpage 的功能是用来在模块中搜索关键字的 -k 选项。例如:

清单 4:为任务定位适当的模块

% pydoc.py -k uuencode
uu - Implementation of the UUencode and UUdecode functions.
% pydoc.py uu
Python Library Documentation: module uu
NAME
  uu - Implementation of the UUencode and UUdecode functions.
[...]

pydoc 除了它的命令行用法之外,还有其它四种“模式”可以显示被生成的同样的文档。

Shell 模式:在 Python 交互式 shell 中,您可以导入 pydoc 的 help() 函数,这样就能够在不离开交互式会话的情况下获得任何对象的帮助。也可以只输入一个 help 进入交互式“help 解释器”。例如:

清单 5:shell 模式下的交互式 help 解释器

#------- Interactive shell with help enhancements ------#
>>> from pydoc import help
>>> import uu
>>> help(uu.test)
Help on function test in module uu:
test()
 uuencode/uudecode main program
>>> help
Welcome to Python 2.0! This is the online help utility.
[...introductory message about help shell...]
help>

上一页  1 2 3 4 5 6 7  下一页

Tags:可爱 Python pydoc

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接