More efficient/elegant branching

musbur at posteo.org musbur at posteo.org
Tue Dec 17 12:46:38 EST 2019


On Fri, 13 Dec 2019 11:34:24 +0100
Antoon Pardon <antoon.pardon at vub.be> wrote:

> Well if you really want to go this route, you may consider the
> following:
> 
> def branch4(a, b, z):
>     decision = [
>         ((lambda: a > 4 and b == 0), "first"),
>         ((lambda: len(z) < 2), "second"),
>         ((lambda: b + a == 10), "third")]
>     for test, result in decision:
>         if test(): return result

Nice. But as I've said before, in this case code legibility is more
important than out-clevering myself, so I'll stick with if/elif.


More information about the Python-list mailing list