pydoc.doc 检索失败 "os.path.sep"

pydoc.doc fail to retrieve the "os.path.sep"

我在交互模式下用os.path.sep查看帮助实用程序,然后通过:

In [59]: help(os.path.sep)
Related help topics: lambda, or, and, not, in, is, BOOLEAN, COMPARISON,BITWISE, SHIFTING, BINARY, FORMATTING, POWER, UNARY, ATTRIBUTES,SUBSCRIPTS, SLICINGS, CALLS, TUPLES, LISTS, DICTIONARIES

Operator precedence


The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).

但是,当我尝试使用 pydoc 检索它时

In [61]: with open("osPathSep_help.md", "w") as fout:
    ...:     pydoc.doc(os.path.sep, output=fout)
    ...:     
No Python documentation found for '/'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

In [62]: os.stat("osPathSep_help.md").st_size
Out[62]: 0

如何获取与引用内容相同的内容?

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most

要了解此行为,您必须进入 os.path 库源代码。如果你进入代码,你会发现所有这些变量

curdir = '.'
pardir = '..'
extsep = '.'
sep = '\'
pathsep = ';'
altsep = '/'
defpath = '.;C:\bin'
devnull = 'nul'

现在您要做的是将 os.path.sep 作为函数访问,但如您所见,它不是函数,而是一个赋值为 '\'[= 的变量41=]

所以 os.path.sep 总是 \ ,这就是为什么当你做 help(os.path.sep) 你得到的结果是

Operator precedence

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).

这将与您执行

时得到的结果相同
help('\')

现在您的下一个问题是它如何与 help 一起使用?

因此,如果您查看帮助的源代码,就会提到它

"""
Define the built-in 'help'.
This is a wrapper around pydoc.help (with a twist).
"""

所以帮助在内部使用 pydoc.help

因此,如果您使用

pydoc.help('\')pydoc.help(os.path.sep) 你会得到你想要的输出。