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 |2 Answers
Reset to default 1This 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
版权声明:本文标题:Shorter way to execute some code if any case were matched Python 3.10 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386723a2465183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
c
is 1 and thus you cannot call it in the foobar case. – JonSG Commented Nov 20, 2024 at 14:37c
for both a function and an int object? – user19077881 Commented Nov 20, 2024 at 20:11go
variable. – Barmar Commented Nov 21, 2024 at 0:26