admin管理员组

文章数量:1335431

I have a match...case... loop set up, and I want to run some code if any one of the cases were matched. I figured I could just do this by having a designated variable like in the following, but I wondered if there was a shorter way to do it, as this seems to be excessively verbose.

Here's some example code that does, in a nutshell, what I need to do:

def foo(x):
    go = 0
    match x:
        case "Hello,":
            a()
            go = 1
        case "World!":
            b()
            go = 1
        case "foobar":
            c()
            go = 1
    if go == 1:
        print("Something happened")
    else:
        print("Something didn't happen :(")

I could also run my function in every case:

        case "Hello,":
            a()
            print("Something happened")
(etc...)

But again, this seems redundant and I wanted to know if there was a more elegant solution.

I have a match...case... loop set up, and I want to run some code if any one of the cases were matched. I figured I could just do this by having a designated variable like in the following, but I wondered if there was a shorter way to do it, as this seems to be excessively verbose.

Here's some example code that does, in a nutshell, what I need to do:

def foo(x):
    go = 0
    match x:
        case "Hello,":
            a()
            go = 1
        case "World!":
            b()
            go = 1
        case "foobar":
            c()
            go = 1
    if go == 1:
        print("Something happened")
    else:
        print("Something didn't happen :(")

I could also run my function in every case:

        case "Hello,":
            a()
            print("Something happened")
(etc...)

But again, this seems redundant and I wanted to know if there was a more elegant solution.

Share Improve this question edited Nov 20, 2024 at 20:15 Klumpy7 asked Nov 20, 2024 at 2:01 Klumpy7Klumpy7 1135 bronze badges 4
  • Note c is 1 and thus you cannot call it in the foobar case. – JonSG Commented Nov 20, 2024 at 14:37
  • 1 Why are you using the same name c for both a function and an int object? – user19077881 Commented Nov 20, 2024 at 20:11
  • Ooops.. My bad. That is an error on my part when trying to think of super creative function names that I could use... – Klumpy7 Commented Nov 20, 2024 at 20:15
  • 1 Use boolean True/False rather than 1/0 for the go variable. – Barmar Commented Nov 21, 2024 at 0:26
Add a comment  | 

2 Answers 2

Reset to default 1

This is a somewhat elegant solution that works if you are running the code in a function:

def foo(x):
    go = True
    match x:
        case "Hello,":
            a()
        case "World!":
            b()
        case "foobar":
            c()
        case _:
            print("Something didn't happen :(")
            go = False
    if go:
        print("Something happened")

This method makes use of the builtin _ case to run code when NONE of the previous cases are met, and sets the variable once.

If you are running this at the end of the function, you can return and skip the need for the variable:

...
        case _:
            print("Something didn't happen :(")
            return
    print("Something happened")

my 2cts

def a():
    print("On 'a' func")

def b():
    print("On 'b' func")

def c():
    print("On 'c' func")

def foo(x):
    if x in cmd:
        cmd[x]()
        print(f"Something happened on '{x}': {cmd[x]}")
    else:
        print(f"Something didn't happen :( on '{x}'")

cmd = {"Hello,": a, "World!": b, "foobar": c}

foo("World!")

foo("bar")
On 'b' func
Something happened on 'World!': <function b at 0x7fd52fe653a0>
Something didn't happen :( on 'bar'

本文标签: Shorter way to execute some code if any case were matched Python 310Stack Overflow