The difference between hard and soft keywords is that hard keywords are always reserved words, even in positions where they make no sense (e.g. x = class + 1), while soft keywords only get a special meaning in context.
[...] The match and case keywords are proposed to be soft keywords, so that they are recognized as keywords at the beginning of a match statement or case block respectively, but are allowed to be used in other places as variable or argument names.
软关键字是上下文敏感的关键字。例如,它允许您使用
class
作为变量名,只要它不能被解释为定义类。例如,它允许使用cls
替换class
今天这是不可能的,因为
class
是一个关键词:考虑到上下文,很明显用户并不打算定义一个新类,而是想要一个名为
class
的标识符Short:软关键字仍然可以用作变量名或参数名。
PEP 622透露了一些信息(我的重点):
我认为最好用演示来解释这一点
async
和await
是Python 3.5和3.6中的软关键字,因此它们可以用作标识符:但在Python 3.7中,它们变成了合适的关键字,只能在有意义的特定上下文中使用:
首先将它们作为软关键字引入的想法主要是不破坏任何使用它们作为标识符的现有代码。同样的道理也适用于即将到来的
match
关键字,例如re.match
和数百万个项目相关问题 更多 >
编程相关推荐