How can I call a sequence of functions until the return value meets some condition?

前端 未结 3 2096
失恋的感觉
失恋的感觉 2021-01-05 04:51

Sometimes I find myself writing code like this:

def analyse(somedata):
    result = bestapproach(somedata)
    if result:
        return result
    else:
            


        
3条回答
  •  清酒与你
    2021-01-05 05:05

    If the number of functions is not too high, why not use the or operator ?

    d = 'somedata'
    result = f1(d) or f2(d) or f3(d) or f4(d)
    

    It will only apply the functions until one of them returns something not False.

提交回复
热议问题