<tfoot id='MYV0a'></tfoot>
  • <i id='MYV0a'><tr id='MYV0a'><dt id='MYV0a'><q id='MYV0a'><span id='MYV0a'><b id='MYV0a'><form id='MYV0a'><ins id='MYV0a'></ins><ul id='MYV0a'></ul><sub id='MYV0a'></sub></form><legend id='MYV0a'></legend><bdo id='MYV0a'><pre id='MYV0a'><center id='MYV0a'></center></pre></bdo></b><th id='MYV0a'></th></span></q></dt></tr></i><div id='MYV0a'><tfoot id='MYV0a'></tfoot><dl id='MYV0a'><fieldset id='MYV0a'></fieldset></dl></div>

      • <bdo id='MYV0a'></bdo><ul id='MYV0a'></ul>
    1. <legend id='MYV0a'><style id='MYV0a'><dir id='MYV0a'><q id='MYV0a'></q></dir></style></legend>

        <small id='MYV0a'></small><noframes id='MYV0a'>

      1. case/switch 语句的 Python 等价物是什么?

        What is the Python equivalent for a case/switch statement?(case/switch 语句的 Python 等价物是什么?)

        <tfoot id='AMlDJ'></tfoot>

        <small id='AMlDJ'></small><noframes id='AMlDJ'>

        <i id='AMlDJ'><tr id='AMlDJ'><dt id='AMlDJ'><q id='AMlDJ'><span id='AMlDJ'><b id='AMlDJ'><form id='AMlDJ'><ins id='AMlDJ'></ins><ul id='AMlDJ'></ul><sub id='AMlDJ'></sub></form><legend id='AMlDJ'></legend><bdo id='AMlDJ'><pre id='AMlDJ'><center id='AMlDJ'></center></pre></bdo></b><th id='AMlDJ'></th></span></q></dt></tr></i><div id='AMlDJ'><tfoot id='AMlDJ'></tfoot><dl id='AMlDJ'><fieldset id='AMlDJ'></fieldset></dl></div>

        <legend id='AMlDJ'><style id='AMlDJ'><dir id='AMlDJ'><q id='AMlDJ'></q></dir></style></legend>
          <bdo id='AMlDJ'></bdo><ul id='AMlDJ'></ul>

                    <tbody id='AMlDJ'></tbody>
                  本文介绍了case/switch 语句的 Python 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否有 Python 等效的 case 语句,例如 VB.NET 或 C# 中可用的示例?

                  Is there a Python equivalent for the case statement such as the examples available in VB.NET or C#?

                  推荐答案

                  Python 3.10 及以上版本

                  在 Python 3.10 中,他们引入了模式匹配.

                  Python 文档中的示例:

                  def http_error(status):
                      match status:
                          case 400:
                              return "Bad request"
                          case 404:
                              return "Not found"
                          case 418:
                              return "I'm a teapot"
                          case _:
                              return "Something's wrong with the internet"
                  

                  在 Python 3.10 之前

                  虽然官方文档很高兴不提供开关,但我看到了一个使用字典的解决方案.

                  Before Python 3.10

                  While the official documentation are happy not to provide switch, I have seen a solution using dictionaries.

                  例如:

                  # define the function blocks
                  def zero():
                      print "You typed zero.
                  "
                  
                  def sqr():
                      print "n is a perfect square
                  "
                  
                  def even():
                      print "n is an even number
                  "
                  
                  def prime():
                      print "n is a prime number
                  "
                  
                  # map the inputs to the function blocks
                  options = {0 : zero,
                             1 : sqr,
                             4 : sqr,
                             9 : sqr,
                             2 : even,
                             3 : prime,
                             5 : prime,
                             7 : prime,
                  }
                  

                  然后调用等效的 switch 块:

                  Then the equivalent switch block is invoked:

                  options[num]()
                  

                  如果您严重依赖失败,这将开始崩溃.

                  This begins to fall apart if you heavily depend on fall through.

                  这篇关于case/switch 语句的 Python 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <small id='YEgtK'></small><noframes id='YEgtK'>

                  • <bdo id='YEgtK'></bdo><ul id='YEgtK'></ul>

                    <tfoot id='YEgtK'></tfoot>
                          <tbody id='YEgtK'></tbody>

                          <i id='YEgtK'><tr id='YEgtK'><dt id='YEgtK'><q id='YEgtK'><span id='YEgtK'><b id='YEgtK'><form id='YEgtK'><ins id='YEgtK'></ins><ul id='YEgtK'></ul><sub id='YEgtK'></sub></form><legend id='YEgtK'></legend><bdo id='YEgtK'><pre id='YEgtK'><center id='YEgtK'></center></pre></bdo></b><th id='YEgtK'></th></span></q></dt></tr></i><div id='YEgtK'><tfoot id='YEgtK'></tfoot><dl id='YEgtK'><fieldset id='YEgtK'></fieldset></dl></div>

                          <legend id='YEgtK'><style id='YEgtK'><dir id='YEgtK'><q id='YEgtK'></q></dir></style></legend>