PEP 622,文字模式说如下:
请注意,由于使用了相等 (__eq__),以及布尔值与整数 0 和 1 之间的等价性,因此以下两者之间没有实际区别:
case True: ... case 1: ...
and True.__eq__(1)
and(1).__eq__(True)
都返回 True,但是当我用 CPython 运行这两个代码片段时,它看起来case True
和case 1
不一样。
$ python3.10
>>> match 1:
... case True:
... print('a') # not executed
...
>>> match True:
... case 1:
... print('a') # executed
...
a
如何1
与True
实际进行比较?