Python 关键字文档和参考资料的可能更改

Possible change to Python Keyword Documentation and reference materials

几年前有人询问并回答了如何获取关键字列表,但这与 Python 中关键字的记录和更新方式有关。在许多语言中,都有一个帮助页面,其中包含每个关键字以及一个超链接以帮助该关键字。我发誓 Python 也有这个,但现在我找不到了。它存在吗?如果是这样,有人可以提供超链接吗?

这 URL 可帮助您从关键字库生成列表: https://docs.python.org/3/library/keyword.html#keyword.kwlist

但是你还是得一个一个关键字去查找帮助文档。在 iPython 和其他 Python 环境中,这并不难。生成列表后,只需在任何关键字上使用 help()。但我很好奇是否有描述的任何地方的帮助页面。它还存在吗?

相关说明:

TrueFalseNone 在 Python 2 和 Python 3 中表现得像关键字,但测试显示它们只是 [= 中的关键字30=] 3. execprint 从 Python 2 关键字转换为内置函数 Python 3 和 Python 3 添加了 nonlocal 关键字.

help() 功能不限于 IPython。它在 IDLE 和命令行解释器中可用。

TrueFalseNone成为Python中的保留字 3.

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> True = 0
>>> True == False
True

但是

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> True = 0
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

所以差异不是疏忽。

在 Python 组织文档站点上,我认为您现在最接近您要查找的内容是 URL。关键字已列出但未超链接:

https://docs.python.org/3/reference/lexical_analysis.html#keywords https://docs.python.org/2/reference/lexical_analysis.html#keywords

也许评论中关于 keywords 库与 help() 相结合的假设是正确的。在这种情况下要知道的命令:

# to get help on any individual keyword
help("some keyword")  # example:  help("yield")

# to find out all keywords or test if something is a keyword
import keywords
print(keyword.kwlist)

# to test if something is a keyword:
keyword.iskeyword("wordToTest")  # example: keyword.iskeyword("yield")

关于帮助文档,其他人已经承担了责任,您可以找到 Python 3 的页面(该页面还提供了 keywords 库的演示)。此网页超链接关键字以帮助有关它们的内容:

https://www.programiz.com/python-programming/keyword-list