except-clause deletes local variable(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 atry ... finally
statement) or to callexc_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 删除局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:except-clause 删除局部变量


- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 沿轴计算直方图 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12