文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python 中switch的实现

python 中switch的实现

时间:2007-09-20  来源:panix

There is currently no switch statement in Python. Often this is not a problem and can be handled through a series of if-elif-else statements. However, there are many other ways to handle the deficiency. The following example shows how to create a simple switch statement in Python:


def a(s):
    print s
def switch(ch):
    try:
      {'1': lambda : a("one"),
       '2': lambda : a("two"),
      '3': lambda : a("three"),
       'a': lambda : a("Letter a")
      }[ch]()
    except KeyError:
      a("Key not Found")


eg:

>>switch('1')
one
>>switch('a')
Letter a
>>switch('b')
Key not Found

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载