【问题标题】:Python "switch-case" dictionary is not executing functions?Python“switch-case”字典不执行函数?
【发布时间】:2021-08-05 16:29:09
【问题描述】:

我已经遵循了许多示例,说明如何在 Python 中创建字典来代替 switch-case 语句,但由于某种原因,我的代码似乎无法正常工作。函数 ViewCommands,当输入“viewCommands”(作为命令)时,它应该像在脚本开头那样执行,但没有被执行。

ViewCommands()

while True:

    print()

    print('?>> What command would you like to execute?:')
    command = input()
    
    print('>>> Executing command: ', command)
    switcher = {
        'viewCommands': ViewCommands,
        'terminate': Terminate,
        'viewSentences': ViewSentences,
        'addSentences': AddSentences
    }
    case = switcher.get(command, '!');

    if case == '!': print('!>> INVALID INPUT - NOT AN OPTION')

当我为每个引用添加括号时......就像这样:

ViewCommands()

while True:

    print()

    print('?>> What command would you like to execute?:')
    command = input()
    
    print('>>> Executing command: ', command)
    switcher = {
        'viewCommands': ViewCommands(),
        'terminate': Terminate(),
        'viewSentences': ViewSentences(),
        'addSentences': AddSentences()
    }
    case = switcher.get(command, '!');

    if case == '!': print('!>> INVALID INPUT - NOT AN OPTION')

...它确实按预期调用,但随后我遇到了一个错误,即 Terminate 被意外调用,尽管它不应该在字典查询中被引用。

有人知道我为什么会遇到这个问题吗?任何建议将不胜感激,并提前感谢!

【问题讨论】:

  • 在第一个代码块中,函数没有被调用,因为你从不调用它。您认为哪段代码实际上会调用该函数?

标签: python switch-statement


【解决方案1】:

在你关闭一个python函数调用的括号)的那一刻,它将被执行,无论它在哪里。这就是在第二段代码中发生在您身上的事情: Terminate() 正在执行,但您不希望它在那里执行。您在上面部分中的代码是要走的路。在case变量赋值后,就可以执行末尾加括号的函数了。


while True:

    print()

    print('?>> What command would you like to execute?:')
    command = input()
    
    print('>>> Executing command: ', command)
    switcher = {
        'viewCommands': ViewCommands,
        'terminate': Terminate,
        'viewSentences': ViewSentences,
        'addSentences': AddSentences
    }
    case = switcher.get(command, '!');

    if case == '!': 
        print('!>> INVALID INPUT - NOT AN OPTION')
    else:
        case()

PS:我无法检查代码,因为您没有提供 ViewCommands、Terminate、ViewSentences 和 AddSentences 的作用,或者它们接收哪些参数。

【讨论】:

    【解决方案2】:

    这是一种使其更短并避免不必要的 if 语句的方法。 Lambda 函数肯定很漂亮不是吗?

    我在它后面加了一个(),因为字典case语句得到的是函数对象,它被后面的()执行,和lambda函数一样。

    ViewCommands()
    
    while True:
    
        print()
    
        print('?>> What command would you like to execute?:')
        command = input()
        
        print('>>> Executing command: ', command)
        switcher = {
            'viewCommands': ViewCommands,
            'terminate': Terminate,
            'viewSentences': ViewSentences,
            'addSentences': AddSentences
        }
        case = switcher.get(command, lambda: print('!>> INVALID INPUT - NOT AN OPTION'))()
    

    【讨论】:

    • 是的,这是一个完美的方法。我试图写一个更简单的解决方案以便能够被理解。
    【解决方案3】:

    在 Python 3.10 出现 match/case 语句之前,你可以定义一个 switch 函数,使代码更可口,更易于维护:

    开关功能是这样的:

    def switch(v): yield lambda *c: v in c
    

    你可以用 if/elif/else ...

    for case in switch(command):
        if   case('viewCommands') : ViewCommands()
        elif case('terminate')    : Terminate()
        elif case('viewSentences'): ViewSentences()
        elif case('addSentences') : AddSentences()
        else: print("bad command")
    

    或者以更类似于 C 的方式...

    for case in switch(command):
        if case('viewCommands') : ViewCommands()  ; break
        if case('terminate')    : Terminate()     ; break
        if case('viewSentences'): ViewSentences() ; break 
        if case('addSentences') : AddSentences()  ; break
    else: print("bad command")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2021-11-30
      • 2015-07-27
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多