More efficient/elegant branching

Marco Sulla mail.python.org at marco.sulla.e4ward.com
Tue Dec 24 18:41:56 EST 2019


I agree with Chris Angelico, branch1 is "the way to go". Maybe you
have to add a default at start, maybe None, and maybe raise an
exception if
res is None. Anyway, despite I'm a pain in the... arse and I usually
activate ALL the possible warnings in the world, I always disable
cyclomatic complexity warnings in all linters, in any language :D

# Off topic - START

I proposed a switch-case some years ago. I was brutally condemned like
an heretic :D It was something like:

when expr match arg1, arg2, ....:
    [...]
match *args:
    [...]
[...]
else: # optional
    [...]

expr was evaluated, and checked if equal to one of the arguments. At
the first positive check, the relative code was evaluated, and the
when breaks. No break keyword, like if-elif. If no check passed and an
else clause is present, its code will be evaluated.

Anyway this is not helpful in your case, since you have more
complicated checks. Mine was only a suggestion to introduce the simple
C switch-case in Python, for making easier trivial if-elif chains and
for code optimization (C switch-case is optimized by C compilers)

# Of topic - END


More information about the Python-list mailing list