python - 为什么Python 3.10中关键字 `match`可以作为变量名或函数名?

标签 python pattern-matching keyword python-3.10

<分区>

我不完全理解为什么关键字match可以用作变量或函数名,不像其他关键字ifwhile等等?

>>> 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 , matchcase 被添加为 "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.

关于python - 为什么Python 3.10中关键字 `match`可以作为变量名或函数名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68744441/

相关文章:

python - 如何在不使用临时文件的情况下在python中的tarfile中写入大量数据

Python minidom 从 XML 中提取文本

c++ - 有什么理由在 C++03 中使用 'auto' 关键字吗?

java - "Throws"有什么作用,它有什么用?

python - 在 Automator 工作流程中传输参数。 Python 外壳

python - 使用 python 从文件中删除字符串

php - 使用正则表达式删除字符串中不需要的字符

rust - 有没有办法在匹配中使用自定义模式,例如正则表达式或函数?

c# - F# 中的模式匹配比 C# 中的 If else/switch 慢得多吗?

C++ 模板默认构造函数