Python里怎么实现switch case?

Python里怎么实现switch case?

阅读 7.8k
5 个回答

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

switch_dict = {
    "case_foo": foo(),
    "case_bar": bar(),
    ...
}
switch_dict.get("case", "default_case")()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题