Eleflea
2019-10-10 10:28:31 +08:00
我觉得这样做没什么问题。Python 的 try...except 不仅是用来处理错误的。参见 fluent python P449.
In Python, try/except is commonly used for control flow, and not just for error han‐
dling. There’s even an acronym/slogan for that documented in the official Python glos‐
sary:
EAFP
Easier to ask for forgiveness than permission. This common Python coding style
assumes the existence of valid keys or attributes and catches exceptions if the as‐
sumption proves false. This clean and fast style is characterized by the presence of
many try and except statements. The technique contrasts with the LBYL style com‐
mon to many other languages such as C.