【发布时间】:2011-09-11 12:36:48
【问题描述】:
我是 python 新手,我非常喜欢使用字典而不是 switch/case 语句的概念,但是有一个问题我想不通
假设我们有一个“pythonic case”语句
{
'a': somemethod,
'b': othermethod
}['a']()
这很好用,但我不知道如何像 java 等其他语言一样运行一些代码块,看起来像这样
{
'a': { some commands here }
'b': { other commands here that are executed }
}['a']
也许 lambda 会有所帮助?
非常感谢
【问题讨论】:
-
Python 没有 switch 语句,放弃尝试嫁接一个
标签: python dictionary lambda case switch-statement