問題描述
如何阻止 Python 刪除名稱綁定,當該名稱是用于綁定捕獲的異常?這是什么時候發生的變化行為進入 Python?
How can I stop Python from deleting a name binding, when that name is used for binding the exception that is caught? When did this change in behaviour come into Python?
我正在編寫在 Python 2 和 Python 3 上運行的代碼:
I am writing code to run on both Python 2 and Python 3:
exc = None
try:
1/0
text_template = "All fine!"
except ZeroDivisionError as exc:
text_template = "Got exception: {exc.__class__.__name__}"
print(text_template.format(exc=exc))
請注意,exc
在異常處理之前被顯式綁定,因此 Python 知道它是外部范圍內的名稱.
Notice that exc
is explicitly bound before the exception handling, so Python knows it is a name in the outer scope.
在 Python 2.7 上,它運行良好,并且 exc
名稱仍然可以用于format
調用::
On Python 2.7, this runs fine and the exc
name survives to be used in
the format
call::
Got exception: ZeroDivisionError
太好了,這正是我想要的:except
子句綁定了名稱我可以在函數的其余部分使用該名稱來引用異常對象.
Great, this is exactly what I want: The except
clause binds the name
and I can use that name in the rest of the function to refer to the
exception object.
在 Python 3.5 上,format
調用失敗,因為顯然 exc
綁定被刪除::
On Python 3.5, the format
call fails because apparently the exc
binding is deleted::
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
NameError: name 'exc' is not defined
為什么 exc
綁定會從外部作用域中刪除?我們是什么意思可靠地保留名稱綁定以在 except
之后使用它子句?
Why is the exc
binding deleted from the outer scope? How are we meant
to reliably preserve the name binding to use it after the except
clause?
此更改何時進入 Python,記錄在哪里?
When did this change come into Python, where is it documented?
將其報告為 Python 3 中的錯誤是否正確?
Would I be right to report this as a bug in Python 3?
推薦答案
不,這不是錯誤.您遇到的行為在 Python 3 中明確定義try
/except
語句 的文檔.也給出了這種行為的原因:
No this is not a bug. The behavior you are experiencing is clearly and explicitly defined in the Python 3 documentation for the try
/except
statement. The reason for this behavior is also given:
當使用 as target
分配異常時,它會在 except
子句的末尾被清除.這好像
When an exception has been assigned using
as target
, it is cleared at the end of theexcept
clause. This is as if
except E as N:
foo
被翻譯成
except E as N:
try:
foo
finally:
del N
這意味著必須將異常分配給不同的名稱,以便能夠在 except
子句之后引用它.異常被清除,因為附加了回溯,它們與堆棧幀形成一個引用循環,使該幀中的所有本地人保持活動狀態,直到下一次垃圾回收發生.
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.
在 try
/except
塊范圍之外聲明名稱不起作用的原因是您在as
子句.這就是 Python 刪除的名稱.
The reason declaring the name outside of the scope of the try
/except
block didn't work is because you used exc
in the as
clause. So that was the name Python deleted.
解決方法是在 as
子句中使用不同的名稱將異常綁定到,然后將全局變量分配給不同的異常名稱:
The fix is to use a different name in the as
clause to bind the exception to, and then assign the global variable to the different exception name:
>>> exc_global = None
>>> try:
1 / 0
text_template = "All fine!"
except ZeroDivisionError as exc:
exc_global = exc
text_template = "Got exception: {exc.__class__.__name__}"
>>> print(text_template.format(exc=exc_global))
Got exception: ZeroDivisionError
正如 Anthony Sottile 在評論中指出的那樣,try
/except
代碼的反匯編也清楚地支持了文檔的上述陳述:
As Anthony Sottile noted in the comments, the disassembly for the try
/except
code also clearly supports the above statements made by the documentation:
>>> code = """
try:
1/0
text_template = "All fine!"
except ZeroDivisionError as exc:
text_template = "Got exception: {exc.__class__.__name__}"
"""
>>> from dis import dis
>>> dis(code)
2 0 SETUP_EXCEPT 16 (to 18)
3 2 LOAD_CONST 0 (1)
4 LOAD_CONST 1 (0)
6 BINARY_TRUE_DIVIDE
8 POP_TOP
4 10 LOAD_CONST 2 ('All fine!')
12 STORE_NAME 0 (text_template)
14 POP_BLOCK
16 JUMP_FORWARD 38 (to 56)
5 >> 18 DUP_TOP
20 LOAD_NAME 1 (ZeroDivisionError)
22 COMPARE_OP 10 (exception match)
24 POP_JUMP_IF_FALSE 54
26 POP_TOP
28 STORE_NAME 2 (exc)
30 POP_TOP
32 SETUP_FINALLY 10 (to 44)
6 34 LOAD_CONST 3 ('Got exception: {exc.__class__.__name__}')
36 STORE_NAME 0 (text_template)
38 POP_BLOCK
40 POP_EXCEPT
42 LOAD_CONST 4 (None)
>> 44 LOAD_CONST 4 (None)
46 STORE_NAME 2 (exc)
48 DELETE_NAME 2 (exc)
50 END_FINALLY
52 JUMP_FORWARD 2 (to 56)
>> 54 END_FINALLY
>> 56 LOAD_CONST 4 (None)
58 RETURN_VALUE
這篇關于`except` 子句中的名稱綁定在子句后刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!