你是否需要总是写出所有2 ^ n种可能性?
你需要做的所有事情都是不同的(所以2 ^ n动作也是如此?)
不过我可以给出一些提示:
不要使用’== True’或’== False’
你写的等于:
if cond_1 and not cond_2:
do something
elif cond_1 and cond_2:
do something else
elif not cond_1 and cond_2:
do this
还要考虑写作:
if cond_1:
if cond_2:
Do something
else:
Do something
else:
if cond_2:
Do something
else:
Do something
您还可以根据值定义函数:
def __init__(self):
self.actions = {
(False, False): action_1,
(False, True ): action_2,
...
}
def action_1(self):
some action
def action_2(self):
some action
....
并称之为:
self.actions[(cond_1, cond_2)]()
如果您想要在具有类似操作的不同条件的情况下优化选项数量,请查看Karnaugh Maps(它比它看起来更容易):