how to use python's any -
i feel confused code this[not written me]:
version = any(func1(), func2()) # wrong, should any([func1(), func2()]) def func1(): if something: return 1 else: return none def func2(): if something: return 2 else: return 3 version must num. when [func1(), func2()] [1, none], should return 1, when [none, 2], should return 2, when [1, 2], should return 1.
so think it's wrong use any() in code, because any() return true or false. if rewirte logic using way, can not find graceful way pythoner.
i want know whether any() can achieve logic, if not, how achieve gracefully?
you can use or here.
version = func1() or func2() make sure functions defined before trying call them.
this works because or returns first true-like value or last value (if no value true-like) . , 'none' considered false-like in boolean context.
Comments
Post a Comment