Python 用字典替换多个if语句的最佳方法

Python 用字典替换多个if语句的最佳方法,python,python-3.x,for-loop,if-statement,Python,Python 3.x,For Loop,If Statement,我有多种情况: if you == 1 or you == 2: one.put(argument) elif you == 3: return None elif you == 4: two.put(argument) elif you == 5: three.put(argument) elif you == 6: four.put(argument) elif you == 7: five.put(argument) elif you ==

我有多种情况:

if you == 1 or you == 2:
    one.put(argument)
elif you == 3:
    return None
elif you == 4:
    two.put(argument)
elif you == 5:
    three.put(argument)
elif you == 6:
    four.put(argument)
elif you == 7:
    five.put(argument)
elif you == 8:
    six.put(argument)
elif you == 9:
    seven.put(argument)
elif you == 10:
    eight.put(argument)
elif you == 11:
    nine.put(argument)
elif you == 12:
    ten.put(argument)
我想将其更改为使用字典,但在以下情况下会出现异常:

if you == 1 or you == 2:
    one.put(argument)
elif you == 3:
    return None
这样做的最佳方法是什么?

这将起作用:

actions = {1: one.put,
           2: one.put,
           3: None,
           4: two.put,
           # ....
           }

action = actions.get(you)
if callable(action):  # guards against non existing "you"'s or if you == 3
    action(argument)

# can also do this:
# if action is not None:
    # action(argument)

# or that..
# try:
#     action(argument)
# except TypeError:  # if action is None we'll get an exception, NoneType isn't callable
#    pass
这将有助于:

actions = {1: one.put,
           2: one.put,
           3: None,
           4: two.put,
           # ....
           }

action = actions.get(you)
if callable(action):  # guards against non existing "you"'s or if you == 3
    action(argument)

# can also do this:
# if action is not None:
    # action(argument)

# or that..
# try:
#     action(argument)
# except TypeError:  # if action is None we'll get an exception, NoneType isn't callable
#    pass

将表达式的不同部分存储在词典中。 我也在里面放了3个,只是为了完整性,以及以后可能的使用

put_dict = {
    1: one, 2: one,
    3: None
    4: two,  5: three,
    6: four, 7: five,
    8: six,  9: seven,
    10: eight, 11: nine,
    12: ten
}

if you == 3:
    return None
else:
    put_dict[you].put(argument)  

将表达式的不同部分存储在词典中。 我也在里面放了3个,只是为了完整性,以及以后可能的使用

put_dict = {
    1: one, 2: one,
    3: None
    4: two,  5: three,
    6: four, 7: five,
    8: six,  9: seven,
    10: eight, 11: nine,
    12: ten
}

if you == 3:
    return None
else:
    put_dict[you].put(argument)  

我会为您不想要的值创建一个接收器:

class Sink:
    @staticmethod
    def put(object):
        pass

put_dict = {
    1: one, 2: one,
    3: Sink,
    4: two,  5: three,
    6: four, 7: five,
    8: six,  9: seven,
    10: eight, 11: nine,
    12: ten}

def function(you, argument)
    put_dict[you].put(argument)

我会为您不想要的值创建一个接收器:

class Sink:
    @staticmethod
    def put(object):
        pass

put_dict = {
    1: one, 2: one,
    3: Sink,
    4: two,  5: three,
    6: four, 7: five,
    8: six,  9: seven,
    10: eight, 11: nine,
    12: ten}

def function(you, argument)
    put_dict[you].put(argument)

你应该问有什么例外?也许吧,但我正在学习。我还不是专业人士developer@DeepSpace从第一行到第四行。不一样,我们在第四行之后使用,所以我写了一篇关于使用python字典而不是过去的if-else语句的博客。你可以读到,你应该问,有什么例外?也许吧,但我正在学习。我还不是专业人士developer@DeepSpace从第一行到第四行。不一样,我们在第四行之后使用,所以我写了一篇关于使用python字典而不是过去的if-else语句的博客。你可以读到它