在一个大库中,我遇到了一个"错误",因为一个函数只接受float但不接受int
def foo(penalty: float):
if not isinstance(penalty, float):
raise ValueError(f"`penalty` has to be a float, but is {penalty}")
# some instruction
我想用以下内容开设公关:
def foo(penalty: int | float):
if not isinstance(penalty, (int,float)):
raise ValueError(f"`penalty` has to be a float or int, but is {penalty}")
# some instruction
但在此之前,我问自己: Python中是否有充分的理由接受float而不接受int?