mardi 13 juillet 2021

Simplifying if-else statement with boolean functional conditions in Python

I have a series of if-else statements like this:

if funct1():
   return A
elif funct2():
   return B
elif funct3():
   return C
... etc

where funct1, funct2, funct3, etc all return booleans

I know that if the conditions of the if-else statements are possible values of a variable than a dictionary can be used to simplify the if-else statement like this:

if foo == A:
  return 1
elif foo == B:
  return 2
... etc

becomes

dict = {A: 1, B:2, ... etc }
return dict[foo]

can something similar be done for my first example involving conditionals that comprise of functions which return booleans?

Aucun commentaire:

Enregistrer un commentaire