I have a function that checks 3 different boolean flags and has a unique outcome for each of the 8 combinations.
"""
a | b | c | out
- + - + - + ---
0 | 0 | 0 | 0
0 | 0 | 1 | 1
0 | 1 | 0 | 2
0 | 1 | 1 | 3
1 | 0 | 0 | 4
1 | 0 | 1 | 5
1 | 1 | 0 | 6
1 | 1 | 1 | 7
"""
def do_stuff(a, b, c):
if a:
if b:
if c:
return function7() # a, b, c
return function6() # a, b, !c
if c:
return function5() # a, !b, c
return function4() # a, !b, !c
else:
if b:
if c:
return function3() # !a, b, c
return function2() # !a, b, !c
if c:
return function1() # !a, !b, c
return function0() # !a, !b, !c
I'm already short-cutting a lot of else statements since I'm returning (exiting the loop).
Is there a more DRY way to accomplish this? I could convert to binary, and do a single depth level of if/elif, but I don't want to use "magic numbers".
Also, I realize this is only 16 lines of code for 8 outcomes, but what if it were 4 variables, is there a way to increase readability/flow?
{0: function0, ...}, etc.?