在 Python 中,我们有 if、elif、else、break、continue 这样的东西、pass 等。为了找个更好的词,我称它们为特殊说明。
它们是什么类型的?如果我执行类似type(pass) 的操作,我会得到一个SyntaxError。
请您参考如下方法:
根据文档,您要查找的术语是 "Keywords" .
2.3.1. Keywords
The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
这些是语言结构。它们是语言的一部分 grammar and syntax ,并且不是对象。口译员以不同的方式对待这些。因此,它们不像对象通常那样与类型相关联。
如果出于任何原因,你想知道一个词是否是 python 关键字(又名,保留字),你可以导入 keyword 模块并测试 -
import keyword
keyword.iskeyword('if')
True
