这是一个执行函数的"pythonic"方法,作为元组值的python switch语句吗?

Ken*_*Ken 5 python

我有一种情况,我有六种可能与四种不同结果有关的情况.而不是使用扩展的if/else语句,我想知道使用字典来调用我在if/else中调用的函数来替换"switch"语句是否会更加pythonic,就像人们可能会使用在C#或php中.

我的switch语句取决于我用来构建元组的两个值,我将把它作为字典的键,用作我的"开关".我将从另外两个函数(数据库调用)中获取元组的值,这就是为什么我有示例one()和zero()函数.

这是我正在考虑使用的代码模式,我偶然发现在python shell中玩:

def one():
    #Simulated database value
    return 1

def zero():
    return 0

def run():
    #Shows the correct function ran
    print "RUN"
    return 1

def walk():
    print "WALK"
    return 1

def main():
    switch_dictionary = {}

    #These are the values that I will want to use to decide
    #which functions to use
    switch_dictionary[(0,0)] = run
    switch_dictionary[(1,1)] = walk

    #These are the tuples that I will build from the database
    zero_tuple = (zero(), zero())
    one_tuple = (one(), one())

    #These actually run the functions. In practice I will simply 
    #have the one tuple which is dependent on the database information
    #to run the function that I defined before
    switch_dictionary[zero_tuple]()
    switch_dictionary[one_tuple]()
Run Code Online (Sandbox Code Playgroud)

我没有实际编写的代码或我将它张贴在这里,因为我想知道,如果这种方法被认为是蟒蛇的最佳实践.我仍然是大学里的蟒蛇学习者,如果这是一种习惯的方法,那么在我走进现实世界之前,我想现在就开始吧.

注意,执行上面代码的结果是预期的,只需"RUN"和"WALK".

编辑

对于那些感兴趣的人,这就是相关代码的结果.它正在谷歌应用程序引擎应用程序上使用.您应该会发现代码比我的粗略示例模式更加整洁.它比我之前复杂的if/else树工作得更好.

def GetAssignedAgent(self):
    tPaypal = PaypalOrder() #Parent class for this function
    tAgents = []
    Switch = {}

    #These are the different methods for the actions to take
    Switch[(0,0)] = tPaypal.AssignNoAgent
    Switch[(0,1)] = tPaypal.UseBackupAgents
    Switch[(0,2)] = tPaypal.UseBackupAgents
    Switch[(1,0)] = tPaypal.UseFullAgents
    Switch[(1,1)] = tPaypal.UseFullAndBackupAgents
    Switch[(1,2)] = tPaypal.UseFullAndBackupAgents
    Switch[(2,0)] = tPaypal.UseFullAgents
    Switch[(2,1)] = tPaypal.UseFullAgents
    Switch[(2,2)] = tPaypal.UseFullAgents

    #I'm only interested in the number up to 2, which is why
    #I can consider the Switch dictionary to be all options available.
    #The "state" is the current status of the customer agent system
    tCurrentState = (tPaypal.GetNumberofAvailableAgents(), 
                     tPaypal.GetNumberofBackupAgents())

    tAgents = Switch[tCurrentState]()
Run Code Online (Sandbox Code Playgroud)

daw*_*awg 14

改为考虑这个习语:

>>> def run():
...   print 'run'
... 
>>> def walk():
...   print 'walk'
... 
>>> def talk():
...    print 'talk'
>>> switch={'run':run,'walk':walk,'talk':talk}
>>> switch['run']()
run
Run Code Online (Sandbox Code Playgroud)

我认为它比你前进的方向更具可读性.

编辑

这也有效:

>>> switch={0:run,1:walk} 
>>> switch[0]()
run
>>> switch[max(0,1)]()
walk
Run Code Online (Sandbox Code Playgroud)

您甚至可以将此习语用于switch / default类型结构:

>>> default_value=1
>>> try:
...    switch[49]()
... except KeyError:
...    switch[default_value]()
Run Code Online (Sandbox Code Playgroud)

或者(可读性更低,更简洁):

>>> switch[switch.get(49,default_value)]()
walk
Run Code Online (Sandbox Code Playgroud)

编辑2

同样的习语,扩展到你的评论:

>>> def get_t1():
...    return 0
... 
>>> def get_t2():
...    return 1
... 
>>> switch={(get_t1(),get_t2()):run}
>>> switch
{(0, 1): <function run at 0x100492d70>}
Run Code Online (Sandbox Code Playgroud)

可读性至关重要