在乘法时,我有这段代码来检查变量是数字还是Vector2在我的Vector2类中。
def __mul__(self, other):
match type(other):
case int | float:
pass
case Vector2:
pass
如果我运行这个,我会得到SyntaxError: name capture 'int' makes remaining patterns unreachable
,当我在vscode中盘旋时,它会给我:
"int" is not accessed
Irrefutable pattern allowed only as the last subpattern in an "or" pattern
All subpatterns within an "or" pattern must target the same names
Missing names: "float"
Irrefutable pattern is allowed only for the last case statement
如果我删除了| float
,它仍然不能工作,所以我不能将它们分开。
发布于 2022-07-31 16:26:38
变量(例如:case _:
或case other:
)的大小写需要是列表中的最后一个case
。它匹配任何值,其中的值不是由以前的情况匹配的,并在变量中捕获该值。
一个类型可以在一种情况下使用,但暗示isinstance()
,测试以确定所匹配的值是否是该类型的实例。因此,用于匹配的值应该是实际变量other
,而不是type(other)
类型,因为type(other)
是一个类型与type()
匹配的类型。
def __mul__(self, other):
match other:
case int() | float():
pass
case Vector2():
pass
https://stackoverflow.com/questions/71982525
复制相似问题