• <small id='1S2g6'></small><noframes id='1S2g6'>

    • <bdo id='1S2g6'></bdo><ul id='1S2g6'></ul>

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

    1. <tfoot id='1S2g6'></tfoot>

      1. 何时在 python 中使用 if vs elif

        when to use if vs elif in python(何时在 python 中使用 if vs elif)

          <small id='66wVt'></small><noframes id='66wVt'>

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

                  <tfoot id='66wVt'></tfoot>
                    <tbody id='66wVt'></tbody>
                1. <legend id='66wVt'><style id='66wVt'><dir id='66wVt'><q id='66wVt'></q></dir></style></legend>

                  本文介绍了何时在 python 中使用 if vs elif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个包含多个条件语句的函数,其中每个分支都被执行,则从函数返回.我应该使用多个 if 语句还是 if/elif/else?例如,假设我有一个函数:

                  If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function:

                  def example(x):
                      if x > 0:
                          return 'positive'
                      if x < 0:
                          return 'negative'
                      return 'zero'
                  

                  这样写更好吗:

                  def example(x):
                      if x > 0:
                          return 'positive'
                      elif x < 0:
                          return 'negative'
                      else:
                          return 'zero'
                  

                  两者都有相同的结果,但一个比另一个更有效或被认为更惯用?

                  Both have the same outcome, but is one more efficient or considered more idiomatic than the other?

                  有几个人说过,在第一个示例中,总是对两个 if 语句进行评估,但在我看来并非如此

                  A couple of people have said that in the first example both if statements are always evaluated, which doesn't seem to be the case to me

                  例如,如果我运行代码:

                  for example if I run the code:

                  l = [1,2,3]
                  
                  def test(a):
                      if a > 0:
                          return a
                      if a > 2:
                          l.append(4)
                  
                  test(5)
                  

                  l 仍将等于 [1,2,3]

                  l will still equal [1,2,3]

                  推荐答案

                  我会将我的评论扩展到答案.

                  I'll expand out my comment to an answer.

                  在所有情况都返回的情况下,这些确实是等价的.在它们之间进行选择变得重要的是更具可读性.

                  In the case that all cases return, these are indeed equivalent. What becomes important in choosing between them is then what is more readable.

                  您的后一个示例使用 elif 结构来明确声明案例是互斥的,而不是依赖于它们隐含地来自返回的事实.这使得这些信息更加明显,因此代码更容易阅读,并且更不容易出错.

                  Your latter example uses the elif structure to explicitly state that the cases are mutually exclusive, rather than relying on the fact they are implicitly from the returns. This makes that information more obvious, and therefore the code easier to read, and less prone to errors.

                  比如说,有人认为还有另一种情况:

                  Say, for example, someone decides there is another case:

                  def example(x):
                      if x > 0:
                          return 'positive'
                      if x == -15:
                          print("special case!")
                      if x < 0:
                          return 'negative'
                      return 'zero'
                  

                  突然之间,如果用户希望这种情况是互斥的,就会出现一个潜在的错误(显然,这在示例中没有多大意义,但在更现实的情况下可能会发生).如果使用了 elif,则可以消除这种歧义,并且该行为对于添加代码的人在他们添加代码时可能正在查看的级别是可见的.

                  Suddenly, there is a potential bug if the user intended that case to be mutually exclusive (obviously, this doesn't make much sense given the example, but potentially could in a more realistic case). This ambiguity is removed if elifs are used and the behaviour is made visible to the person adding code at the level they are likely to be looking at when they add it.

                  如果我遇到您的第一个代码示例,我可能会假设选择使用 ifs 而不是 elifs 意味着这些情况是不是 是互斥的,所以像改变 x 的值这样的事情可能会被用来改变 if 的执行(显然在这种情况下,意图是显而易见的并且是相互的排他性的,但同样,我们谈论的是不太明显的情况 - 并且一致性很好,所以即使在一个简单的例子中,当它很明显时,最好坚持一种方式).

                  If I were to come across your first code example, I would probably assume that the choice to use ifs rather than elifs implied the cases were not mutually exclusive, and so things like changing the value of x might be used to change which ifs execute (obviously in this case the intention is obvious and mutually exclusive, but again, we are talking about less obvious cases - and consistency is good, so even in a simple example when it is obvious, it's best to stick to one way).

                  这篇关于何时在 python 中使用 if vs elif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                2. <legend id='chJJz'><style id='chJJz'><dir id='chJJz'><q id='chJJz'></q></dir></style></legend>
                    <bdo id='chJJz'></bdo><ul id='chJJz'></ul>
                      <tbody id='chJJz'></tbody>

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

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