为什么Python3.10中的关键字`match`可以作为变量名或函数名?
Why keyword `match` in Python 3.10 can be as a variable or function name?
我不完全理解为什么关键字 match
可以用作变量或函数名称,而不像其他关键字 if
、while
等?
>>> match "abc":
... case "abc":
... print('Hello!')
...
Hello!
>>> from re import match
>>> match('A', 'A Something A')
<re.Match object; span=(0, 1), match='A'>
>>> match = '????'
>>> match
'????'
>>> case = 'something'
>>> case
'something'
根据 PEP 622, match
and case
are being added as "soft keywords",因此它们将保持有效标识符:
This PEP is fully backwards compatible: the match
and case
keywords are proposed to be (and stay!) soft keywords, so their use as variable, function, class, module or attribute names is not impeded at all.
我不完全理解为什么关键字 match
可以用作变量或函数名称,而不像其他关键字 if
、while
等?
>>> match "abc":
... case "abc":
... print('Hello!')
...
Hello!
>>> from re import match
>>> match('A', 'A Something A')
<re.Match object; span=(0, 1), match='A'>
>>> match = '????'
>>> match
'????'
>>> case = 'something'
>>> case
'something'
根据 PEP 622, match
and case
are being added as "soft keywords",因此它们将保持有效标识符:
This PEP is fully backwards compatible: the
match
andcase
keywords are proposed to be (and stay!) soft keywords, so their use as variable, function, class, module or attribute names is not impeded at all.