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

        <small id='0hcMi'></small><noframes id='0hcMi'>

        • <bdo id='0hcMi'></bdo><ul id='0hcMi'></ul>

      1. <tfoot id='0hcMi'></tfoot>
      2. Python 是否优化尾递归?

        Does Python optimize tail recursion?(Python 是否优化尾递归?)

          <tfoot id='pwwAv'></tfoot>

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

                <tbody id='pwwAv'></tbody>
                • <bdo id='pwwAv'></bdo><ul id='pwwAv'></ul>

                • <legend id='pwwAv'><style id='pwwAv'><dir id='pwwAv'><q id='pwwAv'></q></dir></style></legend>
                  <i id='pwwAv'><tr id='pwwAv'><dt id='pwwAv'><q id='pwwAv'><span id='pwwAv'><b id='pwwAv'><form id='pwwAv'><ins id='pwwAv'></ins><ul id='pwwAv'></ul><sub id='pwwAv'></sub></form><legend id='pwwAv'></legend><bdo id='pwwAv'><pre id='pwwAv'><center id='pwwAv'></center></pre></bdo></b><th id='pwwAv'></th></span></q></dt></tr></i><div id='pwwAv'><tfoot id='pwwAv'></tfoot><dl id='pwwAv'><fieldset id='pwwAv'></fieldset></dl></div>
                  本文介绍了Python 是否优化尾递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下代码失败并出现以下错误:

                  I have the following piece of code which fails with the following error:

                  RuntimeError: 超出最大递归深度

                  RuntimeError: maximum recursion depth exceeded

                  我试图重写它以允许尾递归优化 (TCO).我相信如果发生了 TCO,这段代码应该是成功的.

                  I attempted to rewrite this to allow for tail recursion optimization (TCO). I believe that this code should have been successful if a TCO had taken place.

                  def trisum(n, csum):
                      if n == 0:
                          return csum
                      else:
                          return trisum(n - 1, csum + n)
                  
                  print(trisum(1000, 0))
                  

                  我应该断定 Python 不会产生任何类型的 TCO,还是只需要以不同的方式定义它?

                  Should I conclude that Python does not do any type of TCO, or do I just need to define it differently?

                  推荐答案

                  没有,以后也不会了 Guidovan Rossum 更喜欢能够有适当的回溯:

                  No, and it never will since Guido van Rossum prefers to be able to have proper tracebacks:

                  尾递归消除 (2009-04-22)

                  Tail Recursion Elimination (2009-04-22)

                  尾调用的最后一句话 (2009-04-27)

                  Final Words on Tail Calls (2009-04-27)

                  您可以通过如下转换手动消除递归:

                  You can manually eliminate the recursion with a transformation like this:

                  >>> def trisum(n, csum):
                  ...     while True:                     # Change recursion to a while loop
                  ...         if n == 0:
                  ...             return csum
                  ...         n, csum = n - 1, csum + n   # Update parameters instead of tail recursion
                  
                  >>> trisum(1000,0)
                  500500
                  

                  这篇关于Python 是否优化尾递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <bdo id='RMh68'></bdo><ul id='RMh68'></ul>
                  • <tfoot id='RMh68'></tfoot>

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

                      <tbody id='RMh68'></tbody>

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

                          2. <legend id='RMh68'><style id='RMh68'><dir id='RMh68'><q id='RMh68'></q></dir></style></legend>