Python 在标准库中有一个几乎等效的 Javadoc,称为 pydoc。
您可以使用命令将其作为 Web 服务器启动
$ python -m pydoc -b
(或者-p 80如果随机端口给你带来麻烦,那么去http://localhost)
这应该会打开一个网络浏览器,让您可以浏览标准库以及您碰巧安装的任何其他包。
请注意,您还可以使用help() 实用程序从 Python 的交互式 shell/REPL 中获取所有这些信息。
>>> help()
假设你想找到函数来处理字符串,例如strip()。使用这两种方法你会如何找到这个函数?
$ python -m pydoc str
或
>>> help(str)
将显示str 类型的帮助,包括其所有方法。
如果您不知道字符串的类型为 str,您可以创建一个并询问其类型:
>>> type("foo")
<class 'str'>
>>> help(type("foo"))
要查看对象属性的更紧凑目录,可以使用
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
但既然你已经知道名字是strip(),你可以就那个对象寻求帮助。
>>> help(str.strip)
这将显示方法签名和文档字符串,如果有的话。
使用 Pydoc 的 Web 服务器,单击起始页上“内置模块”中的 builtins 链接,然后单击 str 链接以查看完全相同的信息,因为 help() 也由 pydoc 提供.
还有一个“搜索”和一个“获取”栏。在“获取”栏中输入 str.strip 会直接进入,就像使用 help(str.strip) 一样。
这是很棒的信息。谢谢。有没有什么地方在网上发布的?这样就不用在本地启动服务器了吗?
我不知道。鉴于 https://docs.python.org 似乎没有什么意义。本地服务器的优势在于它会根据您启动它时使用的解释器准确记录系统上安装的内容,即使您安装了多个 Python 版本(或使用安装了不同软件包的 virtualenvs)。甚至标准库也可能因操作系统或发行版以及(从源代码编译时)编译时可用的 C 库而异。