python中的switch case不起作用,需要另一种模式。[英] switch case in python doesn't work; need another pattern

本文是小编为大家收集整理的关于python中的switch case不起作用,需要另一种模式。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在这里需要代码方面的帮助,我想在 python 中实现 switch case 模式,所以就像一些教程所说的那样,我可以使用字典,但这是我的问题:

  # type can be either create or update or .. 
  message = { 'create':msg(some_data),
              'update':msg(other_data)
              # can have more
            }

  return message(type)

但这对我不起作用,因为 some_data 或 other_data 可以是 None (如果它没有,它会引发错误)并且 msg 函数需要简单(我不想在其中添加一些条件).

这里的问题是函数 msg() 在每次填充字典时都会执行与通常在其他编程语言中的 switch case 模式不同,除非匹配,否则不会执行 case 中的代码.

有没有其他方法可以做到这一点,或者我只需要如果 elif ...

更新:谢谢大家的回复,其实更像这样

message = { 'create': "blabla %s" % msg(some_data),
            'update': "blabla %s" % msg(other_data)
            'delete': "blabla %s" % diff(other_data, some_data)
           }

所以 lambda 在这里不起作用,并且调用的函数也不相同,所以它更像是我需要的一个真正的开关盒,也许我必须考虑另一种模式.

推荐答案

听起来你比你需要的更复杂.你想要简单吗?

if mytype == 'create':
    return msg(some_data)
elif mytype == 'update':
    return msg(other_data)
else:
    return msg(default_data)

你没有必须使用字典和函数引用,仅仅因为你可以.有时,一个无聊的、显式的 if/else 块正是您所需要的.即使是团队中最新的程序员也很清楚,永远不会不必要地调用 msg().我也敢打赌,这将比您正在研究的其他解决方案更快,除非案例数量增加并且 msg() 快如闪电.

本文地址:https://www.itbaoku.cn/post/627700.html

问题描述

I need a help with a code here, i wanted to implement the switch case pattern in python so like some tutorial said , i can use a dictionary for that but here is my problem:

  # type can be either create or update or .. 
  message = { 'create':msg(some_data),
              'update':msg(other_data)
              # can have more
            }

  return message(type)

but it's not working for me because some_data or other_data can be None (it raise an error if it's none) and the msg function need to be simple (i don't want to put some condition in it).

the problem here is that the function msg() is executed in each time for filling the dict unlike the switch case pattern who usually in other programming language don't execute the code in the case unless it's a match.

is there an other way to do this or i just need to do if elif ...

Update: thank you for all the replies, but actually it's more like this

message = { 'create': "blabla %s" % msg(some_data),
            'update': "blabla %s" % msg(other_data)
            'delete': "blabla %s" % diff(other_data, some_data)
           }

so lambda don't work here and not the same function is called, so it's more like a real switch case that i need , and maybe i have to think on an other pattern .

推荐答案

It sounds like you're complicating this more than you need to. You want simple?

if mytype == 'create':
    return msg(some_data)
elif mytype == 'update':
    return msg(other_data)
else:
    return msg(default_data)

You don't have to use dicts and function references just because you can. Sometimes a boring, explicit if/else block is exactly what you need. It's clear to even the newest programmers on your team and won't call msg() unnecessarily, ever. I'm also willing to bet that this will be faster than the other solution you were working on unless the number of cases grows large and msg() is lightning fast.