Python switch case实现操作是怎样的,pythonswitch,Python里怎么实现s


Python里怎么实现switch case?

Python 里没有 Switch 语法,按官方的说法是用 if...elif... 来替代 switch...case

但是可以用 Dictionary 来实现:

def f(x):    return {        'a': 1,        'b': 2,    }[x]

参考:http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python

用if或者 字典 实现应该都没有问题吧。
http://stackoverflow.com/questions/11479816/what-is-the-python-equivalent-for-a-case-switch-statement

python没有switch case。switch case从逻辑上来说像是一个hash表。你可以使用python的字典来实现类似功能。

switch_dict = {    "case_foo": foo(),    "case_bar": bar(),    ...}switch_dict.get("case", "default_case")()

楼主可以参考一下这个
http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python

编橙之家文章,

评论关闭