软件工程

概念

EAFP:easier to ask forgiveness than permission
LBYL:look before you leap

代码

# LBYL
def getUserInfo(user):
    if user == None:
        print('person must be not None!')
    print(user.info)

# EAFP
def getUserInfo(user):
    try:
        print(user.info)
    except NameError:
        print('user must be not null!')

有点和缺点
EAFP优点是逻辑清晰,Python社区推荐使用。
LBYL拥有很多逻辑控制代码,使得结构混乱。
在高并发情况下,if条件如果是个表达式,会造成一致性问题。参考:http://tech.glowing.com/cn/glow-cache-structure

原文地址:https://www.cnblogs.com/allen2333/p/9139954.html