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 assumption 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 common to many other languages such as C.


Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key] can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach.

to be continue…


YouTube: Permission or forgiveness?
What is the EAFP principle in Python?
Are nested try/except blocks in python a good programming practice?
Do we use try,except in every single function?
海之寻趣: LBYL vs. EAFP
v2ex: 关于条件检查,异常,错误码的一些思考

本文地址 EAFP 和 LBYL 转载请注明出处