我有更好的Python文档吗?更有条理?

I there a better Python documentation? More structured?

我想这个问题已经有人问过了,但我没有找到。

我过去曾与 Java 和 PHP 合作过,我相信他们的语言文档结构更好。至少他们 API.

如果你看看 Java 的 API,那真是太棒了。结构良好且可预测。它还可以让您找到您不知道的新东西。我正在考虑这个 https://docs.oracle.com/javase/7/docs/api/ .

PHP 结构不太好,但中音效果很好。我说的是这个:https://www.php.net/manual/en/ .

现在,如果你看到 Python 的等价物(至少我发现 https://docs.python.org/3/index.html ) it feels like a very long tutorial. From my point of view, searching for stuff is difficult and the there isn't a truly hierarchical organization. When you're reading about functions there's also a lot of text describing stuff when I'm really looking for a summary. Take for instance https://docs.python.org/3/library/string.html ,看到关于 "Format String Syntax" 的部分,感觉它应该去别的地方专门致力于该主题。

所以我的问题是:是否有某个地方 Python API 的结构与 JAVA 中的结构相似?

不确定它是否正是您想要的,但在 python REPL 环境中,您可以使用 help,以获得更重要的内容:

如果您输入具体的方法名称,您可以获得更多信息,即 help(str.format):

Python 在标准库中有一个 near-equivalent 的 Javadoc,叫做 pydoc.

您可以使用以下命令将其作为网络服务器启动

$ python -m pydoc -b

(或者 -p 80 如果随机端口给你带来麻烦,那么转到 http://localhost

这应该会打开一个 Web 浏览器,让您可以浏览标准库以及您碰巧安装的任何其他包。


请注意,您还可以使用 help() 实用程序从 Python 的交互式 shell/REPL 中获取所有这些信息。

>>> help()

let's say you wanted to find functions to do stuff on strings, as an example let's say strip(). How would you find this function using either method?

$ 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 服务器,您在起始页的“Built-in 模块”中单击 builtins link,然后单击 str link查看完全相同的信息,因为 help() 也由 pydoc 提供。

还有一个“搜索”和“获取”栏。在“获取”栏中输入 str.strip 将直接转到它,就像使用 help(str.strip).

This is great info. Thanks. Is there somewhere where this is posted online? So you don't have to start a server locally?

据我所知没有。 https://docs.python.org 似乎没有多大意义。本地服务器的优点是它根据你启动它的解释器准确记录你的系统上安装了什么,即使你安装了多个 Python 版本(或者正在使用安装了不同包的 virtualenvs) .甚至标准库也会根据 OS 或发行版以及(从源代码编译时)编译时可用的 C 库而有所不同。