在 Python 中处理重复的零除法异常的最佳做法是什么?
What is the best practice to handle repitetive ZeroDivision exceptions in Python?
我有太多相同的 try/except 语句。编写这样的代码的最佳方法是什么?也许创建一些函数,它采用像 g = k/y 和 return 结果或 0?
这样的语句
try:
g = k/y
except ZeroDivisionError:
g = 0.00
try:
m = e/w
except ZeroDivisionError:
m = 0.00
try:
q = w/h
except ZeroDivisionError:
q = 0.00
try:
f = i/o
except ZeroDivisionError:
f = 0.00
实际检查该条件如何?
g = k/y if y != 0 else 0.0
你可以把它变成一个函数!这里有几种方法,包括计时。
import time
def safe_divide(a, b):
'Divides a/b, returns 0 if b is 0'
return a/b if b != 0 else 0
def safe_divide_try_except(a, b):
'Tries to divide a/b, returns 0 if b is 0'
try:
return a/b
except ZeroDivisionError:
return 0
def time_function(divide_function, function_name, list_of_divisors, divisor_name):
start_time = time.time()
for a, b in list_of_divisors:
divide_function(a, b)
end_time = time.time()
time_elapsed = end_time - start_time
print(f'{function_name} {divisor_name} Time: {time_elapsed}')
exceptions = [(a, 0) for a in range(1000000)]
non_exceptions = [(a, 3) for a in range(100000)]
time_function(safe_divide, "Safe Divide", exceptions, "Zero Divide")
time_function(safe_divide_try_except, "Try Except", exceptions, "Zero Divide")
time_function(safe_divide, "Safe Divide", non_exceptions, "Safe Divide")
time_function(safe_divide_try_except, "Try Except", non_exceptions, "Safe Divide")
输出:
Safe Divide Zero Divide Time: 0.10719704627990723
Try Except Zero Divide Time: 0.34455204010009766
Safe Divide Safe Divide Time: 0.016250133514404297
Try Except Safe Divide Time: 0.01636195182800293
根据我的实验,使用三元运算符处理零情况似乎更快。
您可以创建一个函数来执行此操作并在需要时调用它:
def div_or_zero(a, b):
try:
return a / b
except ZeroDivisionError:
return 0.0
g = div_or_zero(k, y)
m = div_or_zero(e, w)
q = div_or_zero(w, h)
f = div_or_zero(i, o)
我有太多相同的 try/except 语句。编写这样的代码的最佳方法是什么?也许创建一些函数,它采用像 g = k/y 和 return 结果或 0?
这样的语句try:
g = k/y
except ZeroDivisionError:
g = 0.00
try:
m = e/w
except ZeroDivisionError:
m = 0.00
try:
q = w/h
except ZeroDivisionError:
q = 0.00
try:
f = i/o
except ZeroDivisionError:
f = 0.00
实际检查该条件如何?
g = k/y if y != 0 else 0.0
你可以把它变成一个函数!这里有几种方法,包括计时。
import time
def safe_divide(a, b):
'Divides a/b, returns 0 if b is 0'
return a/b if b != 0 else 0
def safe_divide_try_except(a, b):
'Tries to divide a/b, returns 0 if b is 0'
try:
return a/b
except ZeroDivisionError:
return 0
def time_function(divide_function, function_name, list_of_divisors, divisor_name):
start_time = time.time()
for a, b in list_of_divisors:
divide_function(a, b)
end_time = time.time()
time_elapsed = end_time - start_time
print(f'{function_name} {divisor_name} Time: {time_elapsed}')
exceptions = [(a, 0) for a in range(1000000)]
non_exceptions = [(a, 3) for a in range(100000)]
time_function(safe_divide, "Safe Divide", exceptions, "Zero Divide")
time_function(safe_divide_try_except, "Try Except", exceptions, "Zero Divide")
time_function(safe_divide, "Safe Divide", non_exceptions, "Safe Divide")
time_function(safe_divide_try_except, "Try Except", non_exceptions, "Safe Divide")
输出:
Safe Divide Zero Divide Time: 0.10719704627990723
Try Except Zero Divide Time: 0.34455204010009766
Safe Divide Safe Divide Time: 0.016250133514404297
Try Except Safe Divide Time: 0.01636195182800293
根据我的实验,使用三元运算符处理零情况似乎更快。
您可以创建一个函数来执行此操作并在需要时调用它:
def div_or_zero(a, b):
try:
return a / b
except ZeroDivisionError:
return 0.0
g = div_or_zero(k, y)
m = div_or_zero(e, w)
q = div_or_zero(w, h)
f = div_or_zero(i, o)