小编fba*_*ber的帖子

Python转换风格:内部还是外部功能?

我有一个期望在数字类型上运行的函数.我正在读取要从文件操作的数字,所以当我读它们时,它们是字符串,而不是数字.是否更好地使我的函数能够容忍其他类型(下面的选项(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)

python styles

7
推荐指数
2
解决办法
113
查看次数

标签 统计

python ×1

styles ×1