• <small id='4qOoJ'></small><noframes id='4qOoJ'>

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

      1. <legend id='4qOoJ'><style id='4qOoJ'><dir id='4qOoJ'><q id='4qOoJ'></q></dir></style></legend>
      2. return,return None,根本不返回?

        return, return None, and no return at all?(return,return None,根本不返回?)
        <legend id='NigIJ'><style id='NigIJ'><dir id='NigIJ'><q id='NigIJ'></q></dir></style></legend>

        • <tfoot id='NigIJ'></tfoot>
            <bdo id='NigIJ'></bdo><ul id='NigIJ'></ul>

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

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

                  本文介绍了return,return None,根本不返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑三个函数:

                  def my_func1():
                    print "Hello World"
                    return None
                  
                  def my_func2():
                    print "Hello World"
                    return
                  
                  def my_func3():
                    print "Hello World"
                  

                  它们似乎都返回 None.这些函数的返回值的行为方式有什么不同吗?是否有任何理由偏爱其中一种?

                  They all appear to return None. Are there any differences between how the returned value of these functions behave? Are there any reasons to prefer one versus the other?

                  推荐答案

                  在实际行为上,没有区别.他们都返回 None 就是这样.然而,所有这些都有时间和地点.以下说明基本上是应该如何使用不同的方法(或者至少是我被教导应该如何使用它们),但它们不是绝对规则,所以如果你觉得有必要可以将它们混合起来.

                  On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

                  这表明该函数确实要返回一个值以供以后使用,在这种情况下它返回 None.这个值 None 然后可以在其他地方使用.如果函数没有其他可能的返回值,则永远不会使用 return None.

                  This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

                  在以下示例中,如果给出的 person 是人类,我们将返回 personmother.如果它不是人类,我们返回 None,因为 person 没有 mother(假设它不是动物或其他东西).

                  In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

                  def get_mother(person):
                      if is_human(person):
                          return person.mother
                      else:
                          return None
                  

                  使用 return

                  这与循环中的 break 的使用原因相同.返回值无关紧要,您只想退出整个函数.它在某些地方非常有用,即使您并不经常需要它.

                  Using return

                  This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

                  我们有 15 个囚犯,我们知道其中一人拿着刀.我们一个一个地循环遍历每个prisoner,检查他们是否有刀.如果我们用刀打人,我们可以退出函数,因为我们知道只有一把刀,没有理由检查其余的prisoners.如果我们没有找到带刀的prisoner,我们会发出警报.这可以通过许多不同的方式完成,使用 return 可能甚至不是最好的方式,但这只是展示如何使用 return 退出函数的示例.

                  We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

                  def find_prisoner_with_knife(prisoners):
                      for prisoner in prisoners:
                          if "knife" in prisoner.items:
                              prisoner.move_to_inquisition()
                              return # no need to check rest of the prisoners nor raise an alert
                      raise_alert()
                  

                  注意:你不应该这样做 var = find_prisoner_with_knife(),因为返回值并不意味着被捕获.

                  Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

                  这也将返回 None,但该值并不打算被使用或捕获.它只是意味着函数成功结束.与 C++ 或 Java 等语言的 void 函数中的 return 基本相同.

                  This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

                  在下面的例子中,我们设置了人的母亲的名字,然后函数完成后退出.

                  In the following example, we set person's mother's name and then the function exits after completing successfully.

                  def set_mother(person, mother):
                      if is_human(person):
                          person.mother = mother
                  

                  注意:你不应该这样做 var = set_mother(my_person, my_mother),因为返回值并不意味着被捕获.

                  Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

                  这篇关于return,return None,根本不返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

                        <legend id='WzkgX'><style id='WzkgX'><dir id='WzkgX'><q id='WzkgX'></q></dir></style></legend>
                          <tbody id='WzkgX'></tbody>

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