Python 少用 else 让程式逻辑更清楚
Why
让主逻辑简洁易懂。
What
程式逻辑常出现分支,这时会习惯性地用 if ... elif ... else ... 的写法。虽然这样写起来很直观,却不易读,容易分心在杂乱的分枝里,而无法专心理解全局。
How
使用 Return Guard
def foo(x):
if x < 0:
return False
...
return True
使用函式和 list comprehension
abs_numbers = [math.fabs(x) for x in numbers]
def x_to_y(x):
if SOME_CONDITION:
return SOME_VALUE
...
return SOME_VALUE
ys = [x_to_y(x) for x in xs]
使用 Dictionary
import operator
def calculation(a, op, b):
digits = ['zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
s2n = dict(zip(digits, range(10)))
n2s = dict(zip(range(10), digits))
op_names = ['add', 'subtract', 'divide', 'multiply']
op_funcs = [operator.add, operator.sub, operator.div, operator.mul]
s2op = dict(zip(op_names, op_funcs))
result = s2op[op](s2n[a], s2n[b])
return n2s[result]
print calculation('one', 'add', 'two') # three