假设我有以下代码:
#!/usr/bin/env python3
from torch import Tensor
from typing import Literal
def reduce(x: Tensor, reduction: Literal["mean", "sum", "none"] = "mean") -> Tensor:
if reduction == "mean":
return x.mean()
elif reduction == "sum":
return x.sum()
elif reduction == "none":
return x
else:
raise Exception(f"Invalid choice: {reduction!r}.")
我只是想知道在这种情况下应该使用内置Python异常的规范选择(代替上面编码的
Exception
)。是ValueError
吗?还是会是别的什么?