Python for/while else

和常见的语言不同,Pythonfor/while 可以配合 else 使用。简单来说,当 for/while 循环体中没有执行 break 时,就会执行 else 中的代码。假设需要判断数组中是否存在某个数,如果不存在的话则抛出异常,一种可能的写法是:

1
2
3
4
5
6
7
8
9
10
11
target = 10
found = False

for i in range(5):
if i == target:
found = True

break

if not found:
raise Exception('not found')

借助 for/while else 可改写成:

1
2
3
4
5
6
7
target = 10

for i in range(5):
if i == target:
break
else:
raise Exception('not found')

虽然代码少了几行,但是对于不熟悉该语法特性的人来说可能无法一眼看穿代码的意图。

参考: