简体   繁体   English

Python “switch-case”字典没有执行功能?

[英]Python "switch-case" dictionary is not executing functions?

I have followed many examples of how to create a dictionary in lieu of a switch-case statement in Python, but for some reason, my code does not seem to be working.我已经遵循了许多示例,说明如何创建字典来代替 Python 中的 switch-case 语句,但由于某种原因,我的代码似乎无法正常工作。 function ViewCommands, which should execute as it does at the beginning of the script when "viewCommands" is inputted (as command), is not being executed. function 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')

When I add parentheses to each of the references... like so:当我为每个引用添加括号时......就像这样:

在此处输入图像描述

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')

...it does call as expected, but then I encounter a bug where Terminate is being called unexpectedly, despite how it should not have been referenced in the dictionary query. ...它确实按预期调用,但随后我遇到了一个错误,即 Terminate 被意外调用,尽管它不应该在字典查询中被引用。

Does anyone know why I am encountering this issue?有谁知道我为什么会遇到这个问题? Any advice would be appreciated, and thanks in advance!任何建议将不胜感激,并在此先感谢!

In the exact moment that you close a parenthesis ) on a python function call, it will be executed, no matter where it is.在 python function 调用上关闭括号)那一刻,无论它在哪里,它都会被执行。 This is what is happening to you in the second chunk of code: Terminate() is being executed, but you don't want it to be executed there.这就是在第二段代码中发生在您身上的事情: Terminate() 正在执行,但您不希望它在那里执行。 The code you have in the section above is the way to go.您在上一节中的代码是通往 go 的方法。 After the asignation of the case variable, you can execute the function adding a parenthesis at the end.赋值完case变量后,可以执行function最后加括号。


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: I can't check the code as you haven't provided what ViewCommands, Terminate, ViewSentences and AddSentences do, or which arguments do they receive. PS:我无法检查代码,因为您没有提供 ViewCommands、Terminate、ViewSentences 和 AddSentences 做了什么,或者他们收到了哪些 arguments。

This is a way to make it shorter and avoid unnecessary if statements.这是一种使其更短并避免不必要的 if 语句的方法。 Lambda functions sure are beautiful aren't they? Lambda 功能肯定很漂亮不是吗?

I have added a () after it, as what the dictionary case statement gets is the function object, which gets executed by the () after it, same with the lambda function. I have added a () after it, as what the dictionary case statement gets is the function object, which gets executed by the () after it, same with the lambda function.

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'))()

Until Python 3.10 comes out with the match/case statement, you can define a switch function that can make the code more palatable and easier to maintain:在 Python 3.10 出现 match/case 语句之前,您可以定义一个开关 function 可以使代码更可口且更易于维护:

the switch function is this:开关 function 是这样的:

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

You can use it with if/elif/else...您可以将它与 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")

or in a more C-like fashion...或者以更类似于 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM