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

查看:84
本文介绍了python中的开关盒不起作用;需要另一种模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个代码帮助在这里,我想在python中实现切换案例模式,所以像一些教程说,我可以使用一个字典,但这是我的问题:

 #可以创建或更新#
message = {'create':msg(some_data),
'update' msg(other_data)
#可以有更多
}

返回消息(类型)

但它不适用于我,因为some_data或other_data可以是None(如果没有,它会引发错误),并且msg函数需要简单(我不想把一些条件放在这个问题在这里是每次执行函数msg(),以填充dict
,而不像通常在其他的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)

您不要使用使用dict和函数引用,因为您可以可以。有时一个无聊,明确的 if / else 块正是你需要的。很明显,即使是最新的程序员在你的团队,也不会不必要地调用msg()。我也愿意打赌,这将比您正在开发的其他解决方案更快,除非病例数量增加,而且msg()闪电很快。


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.

这篇关于python中的开关盒不起作用;需要另一种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆