我有一个期望在数字类型上运行的函数.我正在读取要从文件操作的数字,所以当我读它们时,它们是字符串,而不是数字.是否更好地使我的函数能够容忍其他类型(下面的选项(A)),或者在调用函数之前转换为数字(下面的选项(B))?
# Option (A)
def numeric_operation(arg):
i = int(arg)
# do something numeric with i
# Option (B)
def numeric_operation(arg):
# expect caller to call numeric_operation(int(arg))
# do something numeric with arg
Run Code Online (Sandbox Code Playgroud)