Python 3 异常删除封闭范围内的变量,原因不明
Python 3 exception deletes variable in enclosing scope for unknown reason
我有以下代码:
def foo():
e = None
try:
raise Exception('I wish you would except me for who I am.')
except Exception as e:
print(e)
print(e)
foo()
在 Python 2.7 中,这按预期运行并打印:
I wish you would except me for who I am.
I wish you would except me for who I am.
然而在Python3.x中,第一行被打印出来,第二行没有。它似乎删除了封闭范围内的变量,从最后一个打印语句中给我以下回溯:
Traceback (most recent call last):
File "python", line 9, in <module>
File "python", line 7, in foo
UnboundLocalError: local variable 'e' referenced before assignment
这几乎就像是在 except
块之后插入了一条 del e
语句。这种行为有什么理由吗?如果 Python 开发人员希望 except 块有自己的本地作用域,而不是泄漏到周围的作用域,我可以理解,但是为什么它必须删除先前分配的外部作用域中的变量?
When an exception has been assigned using as target
, it is cleared at the end of the except clause. This is as if
except E as N:
foo
was translated to
except E as N:
try:
foo
finally:
del N
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.
这两个 PEP 中都有介绍。
我有以下代码:
def foo():
e = None
try:
raise Exception('I wish you would except me for who I am.')
except Exception as e:
print(e)
print(e)
foo()
在 Python 2.7 中,这按预期运行并打印:
I wish you would except me for who I am.
I wish you would except me for who I am.
然而在Python3.x中,第一行被打印出来,第二行没有。它似乎删除了封闭范围内的变量,从最后一个打印语句中给我以下回溯:
Traceback (most recent call last):
File "python", line 9, in <module>
File "python", line 7, in foo
UnboundLocalError: local variable 'e' referenced before assignment
这几乎就像是在 except
块之后插入了一条 del e
语句。这种行为有什么理由吗?如果 Python 开发人员希望 except 块有自己的本地作用域,而不是泄漏到周围的作用域,我可以理解,但是为什么它必须删除先前分配的外部作用域中的变量?
When an exception has been assigned using
as target
, it is cleared at the end of the except clause. This is as ifexcept E as N: foo
was translated to
except E as N: try: foo finally: del N
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.
这两个 PEP 中都有介绍。