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

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

        <bdo id='Nt4np'></bdo><ul id='Nt4np'></ul>
      1. <small id='Nt4np'></small><noframes id='Nt4np'>

        except-clause 删除局部变量

        except-clause deletes local variable(except-clause 删除局部变量)
        <i id='Ccy8f'><tr id='Ccy8f'><dt id='Ccy8f'><q id='Ccy8f'><span id='Ccy8f'><b id='Ccy8f'><form id='Ccy8f'><ins id='Ccy8f'></ins><ul id='Ccy8f'></ul><sub id='Ccy8f'></sub></form><legend id='Ccy8f'></legend><bdo id='Ccy8f'><pre id='Ccy8f'><center id='Ccy8f'></center></pre></bdo></b><th id='Ccy8f'></th></span></q></dt></tr></i><div id='Ccy8f'><tfoot id='Ccy8f'></tfoot><dl id='Ccy8f'><fieldset id='Ccy8f'></fieldset></dl></div>
          <bdo id='Ccy8f'></bdo><ul id='Ccy8f'></ul>
          <tfoot id='Ccy8f'></tfoot>

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

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

                • 本文介绍了except-clause 删除局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  exc = None
                  try:
                      raise Exception
                  except Exception as exc:
                      pass
                  
                  # ...
                  
                  print(exc)
                  

                  NameError: name 'exc' is not defined

                  This used to work in Python2. Why was it changed this way? If I could at least re-assign to exc, similar to class-level attributes

                  class Foo(object):
                      Bar = Bar
                  

                  but this does not make it work either:

                  exc = None
                  try:
                      raise Exception
                  except Exception as exc:
                      exc = exc
                  

                  Any good hints to achieve the same? I'd prefer not to write something like this:

                  exc = None
                  try:
                      raise Exception("foo")
                  except Exception as e:
                      exc = e
                  
                  # ...
                  
                  print(exc)
                  

                  解决方案

                  The try statement explictily limits the scope of the bound exception, to prevent circular references causing it to leak. See the try statement documentation:

                  When an exception has been assigned using as target, it is cleared at the end of the except clause.

                  [...]

                  This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.

                  Emphasis mine; note that your only option is to bind the exception to a new name.

                  In Python 2, exceptions did not have a reference to the traceback, which is why this was changed.

                  However, even in Python 2, you are explicitly warned about cleaning up tracebacks, see sys.exc_info():

                  Warning: Assigning the traceback return value to a local variable in a function that is handling an exception will cause a circular reference. This will prevent anything referenced by a local variable in the same function or by the traceback from being garbage collected. Since most functions don’t need access to the traceback, the best solution is to use something like exctype, value = sys.exc_info()[:2] to extract only the exception type and value. If you do need the traceback, make sure to delete it after use (best done with a try ... finally statement) or to call exc_info() in a function that does not itself handle an exception.

                  If you do re-bind the exception, you may want to clear the traceback explicitly:

                  try:
                      raise Exception("foo")
                  except Exception as e:
                      exc = e
                      exc.__traceback__ = None
                  

                  这篇关于except-clause 删除局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <tfoot id='o7hDf'></tfoot>
                      <tbody id='o7hDf'></tbody>
                  1. <legend id='o7hDf'><style id='o7hDf'><dir id='o7hDf'><q id='o7hDf'></q></dir></style></legend>
                    • <bdo id='o7hDf'></bdo><ul id='o7hDf'></ul>
                        • <small id='o7hDf'></small><noframes id='o7hDf'>

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