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

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

      2. 为什么我不能将收益与回报一起使用?

        Why can#39;t I use yield with return?(为什么我不能将收益与回报一起使用?)
          <tbody id='TnxPG'></tbody>
        1. <legend id='TnxPG'><style id='TnxPG'><dir id='TnxPG'><q id='TnxPG'></q></dir></style></legend>

              <bdo id='TnxPG'></bdo><ul id='TnxPG'></ul>
              <tfoot id='TnxPG'></tfoot>

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

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

                  本文介绍了为什么我不能将收益与回报一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望您考虑以下代码:

                  I would like you to consider the following code:

                  def func(alist):
                      if len(alist) == 1:
                          return arg * 2
                      for item in alist:
                          yield item * 2
                  

                  当我运行它时,我得到了这个错误:

                  When I run it, I get this error:

                  SyntaxError: 'return' with argument inside generator
                  

                  现在,我意识到我无法做到这一点.但是,我想知道为什么.导致 Python 抛出 SyntaxError 的幕后究竟发生了什么?

                  Now, I realize that I cannot do this. However, I would like to know why. What exactly is going on behind the scenes that is causing Python to throw the SyntaxError?

                  推荐答案

                  Python 必须在字节码编译时判断一个函数是否是生成器.这是因为生成器的语义表明生成器函数中的任何代码都不会在第一次 next 调用之前运行;生成器函数返回一个生成器迭代器,当调用 next 时,它会运行生成器代码.因此,Python 无法通过运行一个函数来决定它是否应该是生成器,直到它遇到 yieldreturn;相反,函数中存在 yield 表明该函数是生成器.

                  Python has to decide whether a function is a generator at bytecode compilation time. This is because the semantics of generators say that none of the code in a generator function runs before the first next call; the generator function returns a generator iterator that, when next is called, runs the generator code. Thus, Python can't decide whether a function should be a generator or not by running it until it hits a yield or a return; instead, the presence of a yield in a function signals that the function is a generator.

                  这篇关于为什么我不能将收益与回报一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                • <i id='ouzhH'><tr id='ouzhH'><dt id='ouzhH'><q id='ouzhH'><span id='ouzhH'><b id='ouzhH'><form id='ouzhH'><ins id='ouzhH'></ins><ul id='ouzhH'></ul><sub id='ouzhH'></sub></form><legend id='ouzhH'></legend><bdo id='ouzhH'><pre id='ouzhH'><center id='ouzhH'></center></pre></bdo></b><th id='ouzhH'></th></span></q></dt></tr></i><div id='ouzhH'><tfoot id='ouzhH'></tfoot><dl id='ouzhH'><fieldset id='ouzhH'></fieldset></dl></div>

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

                    <tbody id='ouzhH'></tbody>

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

                          <tfoot id='ouzhH'></tfoot>
                            <bdo id='ouzhH'></bdo><ul id='ouzhH'></ul>