mercredi 28 juillet 2021

How do I refactor multiple if/elif/else statements when I have complex conditions?

I have something like this below which is repeated in many places in the codebase. Is there a way to consolidate it? I had a look at this, and it is essentially the same question, but with more complex conditions.

if something < value1:
    my_fun1()
elif value1 <= something < value2 and something_else == another_value:
    my_fun2()
elif value2 <= something <= value3 and something_else == another_value:
    my_fun3()
...
else:
    fallback_fun()

Aucun commentaire:

Enregistrer un commentaire