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

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

    1. <tfoot id='Ddx9I'></tfoot>
      • <bdo id='Ddx9I'></bdo><ul id='Ddx9I'></ul>
    2. <legend id='Ddx9I'><style id='Ddx9I'><dir id='Ddx9I'><q id='Ddx9I'></q></dir></style></legend>
      1. python3中的可选yield或return.如何?

        时间:2023-09-02

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

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

            <bdo id='qyTrF'></bdo><ul id='qyTrF'></ul>
              <tbody id='qyTrF'></tbody>

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

                  <tfoot id='qyTrF'></tfoot>
                • 本文介绍了python3中的可选yield或return.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一个可以选择返回或产生结果的函数.这是一个例子.

                  I would like to have a function that can, optionally, return or yield the result. Here is an example.

                  def f(option=True):
                      ...
                      for...:
                          if option:
                              yield result
                          else:
                              results.append(result)
                  
                      if not option:
                          return results
                  

                  当然,这是行不通的,我用 python3 试过了,不管我设置什么选项值,我总是得到一个生成器.据我了解,python 检查函数的主体,如果存在 yield,则结果将是生成器.有什么办法可以解决这个问题并制作一个可以随意返回或屈服的函数?

                  Of course, this doesn't work, I have tried with python3 and I always get a generator no matter what option value I set. As far I have understood, python checks the body of the function and if a yield is present, then the result will be a generator. Is there any way to get around this and make a function that can return or yield at will?

                  推荐答案

                  你不能.任何 yield 的使用都会使函数成为生成器.

                  You can't. Any use of yield makes the function a generator.

                  您可以使用一个使用 list() 将生成器生成的所有值存储在列表对象中并返回的函数来包装您的函数:

                  You could wrap your function with one that uses list() to store all values the generator produces in a list object and returns that:

                  def f_wrapper(option=True):
                      gen = f()
                      if option:
                          return gen    # return the generator unchanged
                      return list(gen)  # return all values of the generator as a list
                  

                  但是,一般来说,这是糟糕的设计.不要让你的函数像这样改变行为;坚持一种返回类型(生成器一个对象),不要让它在两者之间切换.

                  However, generally speaking, this is bad design. Don't have your functions alter behaviour like this; stick to one return type (a generator or an object) and don't have it switch between the two.

                  考虑将其拆分为两个函数:

                  Consider splitting this into two functions instead:

                  def f():
                      yield result
                  
                  def f_as_list():
                      return list(f())
                  

                  如果您需要生成器,请使用 f(),如果您想要列表,请使用 f_as_list().

                  and use either f() if you need the generator, and f_as_list() if you want to have a list instead.

                  由于 list() (和 next() 仅访问生成器的一个值)是内置函数,因此您很少需要使用包装器.直接调用这些函数即可:

                  Since list(), (and next() to access just one value of a generator) are built-in functions, you rarely need to use a wrapper. Just call those functions directly:

                  # access elements one by one
                  gen = f()
                  one_value = next(gen)
                  
                  # convert the generator to a list
                  all_values = list(f())
                  

                  这篇关于python3中的可选yield或return.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:你如何使用:isalnum、isdigit、isupper 来测试字符串的每个字符? 下一篇:如何从函数python返回一个int值

                  相关文章

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

                  <tfoot id='BkCav'></tfoot>
                    <legend id='BkCav'><style id='BkCav'><dir id='BkCav'><q id='BkCav'></q></dir></style></legend>

                  1. <small id='BkCav'></small><noframes id='BkCav'>