结论
-
在 Python 中,不要多想,
keyword
就是“关键字” -
Python3.4 有 33 个关键字
-
Python3.7 有 35 个关键字
-
官网中点击
What’s New In Python 3.7
,查找keywords
就能看到增加的两个关键字
查看
方式一
>>> help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
>>>
>>> help(None) # 三个大写开头的直接用 help 即可
Help on NoneType object:
class NoneType(object)
| Methods defined here:
|
| __bool__(self, /)
| self != 0
|
| __repr__(self, /)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
>>>
>>> help("break") # 首字母非大写的需要先变成字符串,再 help
The "break" statement
*********************
break_stmt ::= "break"
"break" may only occur syntactically nested in a "for" or "while"
loop, but not nested in a function or class definition within that
loop.
It terminates the nearest enclosing loop, skipping the optional "else"
clause if the loop has one.
If a "for" loop is terminated by "break", the loop control target
keeps its current value.
When "break" passes control out of a "try" statement with a "finally"
clause, that "finally" clause is executed before really leaving the
loop.
Related help topics: while, for
>>>
方式二
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>>
补充
- 对 Java 而言,“关键字”与“保留字”有所不同
goto
,const
等是“保留字”但不是“关键字”- 简单地说,它们不能作为标识符,但也不是关键字
- 这些“保留字”,就像有些电影公司买下
IP
,自己不拍,别人也不能拍 - 翻译关系,也有“预留字”的说法,有时候指关键字,有时候指保留字,视具体情况而定了