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

        <small id='41VTJ'></small><noframes id='41VTJ'>

        <tfoot id='41VTJ'></tfoot>
      1. <legend id='41VTJ'><style id='41VTJ'><dir id='41VTJ'><q id='41VTJ'></q></dir></style></legend>
      2. 在 Python 3 中 generator.next() 是可见的吗?

        Is generator.next() visible in Python 3?(在 Python 3 中 generator.next() 是可见的吗?)
      3. <legend id='tDoFF'><style id='tDoFF'><dir id='tDoFF'><q id='tDoFF'></q></dir></style></legend>

      4. <small id='tDoFF'></small><noframes id='tDoFF'>

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

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

                  <bdo id='tDoFF'></bdo><ul id='tDoFF'></ul>
                • 本文介绍了在 Python 3 中 generator.next() 是可见的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个生成系列的生成器,例如:

                  I have a generator that generates a series, for example:

                  def triangle_nums():
                      '''Generates a series of triangle numbers'''
                      tn = 0
                      counter = 1
                      while True:
                          tn += counter
                          yield tn
                          counter += + 1
                  

                  在 Python 2 中,我可以进行以下调用:

                  In Python 2 I am able to make the following calls:

                  g = triangle_nums()  # get the generator
                  g.next()             # get the next value
                  

                  但是在 Python 3 中,如果我执行相同的两行代码,则会收到以下错误:

                  however in Python 3 if I execute the same two lines of code I get the following error:

                  AttributeError: 'generator' object has no attribute 'next'
                  

                  但是,循环迭代器语法在 Python 3 中确实有效

                  but, the loop iterator syntax does work in Python 3

                  for n in triangle_nums():
                      if not exit_cond:
                         do_something()...
                  

                  我还没有找到任何东西来解释 Python 3 的这种行为差异.

                  I haven't been able to find anything yet that explains this difference in behavior for Python 3.

                  推荐答案

                  g.next() 已重命名为 g.__next__().这样做的原因是一致性:像 __init__()__del__() 这样的特殊方法都有双下划线(或当前白话中的dunder"),而 .next() 是该规则的少数例外之一.这已在 Python 3.0 中修复.[*]

                  g.next() has been renamed to g.__next__(). The reason for this is consistency: special methods like __init__() and __del__() all have double underscores (or "dunder" in the current vernacular), and .next() was one of the few exceptions to that rule. This was fixed in Python 3.0. [*]

                  但不要调用 g.__next__(),而是使用 next(g).

                  But instead of calling g.__next__(), use next(g).

                  [*] 还有其他特殊属性已得到此修复;func_name,现在是 __name__,等

                  [*] There are other special attributes that have gotten this fix; func_name, is now __name__, etc.

                  这篇关于在 Python 3 中 generator.next() 是可见的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  <i id='KPZkn'><tr id='KPZkn'><dt id='KPZkn'><q id='KPZkn'><span id='KPZkn'><b id='KPZkn'><form id='KPZkn'><ins id='KPZkn'></ins><ul id='KPZkn'></ul><sub id='KPZkn'></sub></form><legend id='KPZkn'></legend><bdo id='KPZkn'><pre id='KPZkn'><center id='KPZkn'></center></pre></bdo></b><th id='KPZkn'></th></span></q></dt></tr></i><div id='KPZkn'><tfoot id='KPZkn'></tfoot><dl id='KPZkn'><fieldset id='KPZkn'></fieldset></dl></div>

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

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

                          <bdo id='KPZkn'></bdo><ul id='KPZkn'></ul>
                            <tbody id='KPZkn'></tbody>
                          <tfoot id='KPZkn'></tfoot>