如何修复 TypeError: 调用的匹配模式必须是 Python 3.10 中的类型

use*_*098 7 pattern-matching python-3.10

尝试学习 Python 3.10 模式匹配。阅读 8.6.4.9 后尝试了这个例子。映射模式

>>> match 0.0:
...  case int(0|1):
...   print(1)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: called match pattern must be a type
>>>
Run Code Online (Sandbox Code Playgroud)

特别是关于内置类型的注释,包括 int。我应该如何编码来测试整数值 0 或 1(文档中的示例)而不出现此错误?

use*_*098 6

我陷入了困境:

match 0.0
  case int:
    print(1)
Run Code Online (Sandbox Code Playgroud)

有效地重新定义了 int,所以下次我尝试发布的匹配时,它失败了,因为我的int 遮盖了内置

  • 呃,这看起来就像是在等待引入难以调试的问题。 (2认同)