Binding Names in the Exclusion Section Deleted after the Sentence

How can I stop Python from removing a name binding when this name is used to bind an excluded object? When does this behavior change go into Python?

I am writing code to run in 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))

Note that it is excexplicitly bound before exception handling, so Python knows that this name is in the outer scope.

In Python 2.7, this works fine, and the name is excsaved for use in the call format:

Got exception: ZeroDivisionError

Great, this is exactly what I want: the sentence exceptbinds 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 ::

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
NameError: name 'exc' is not defined

exc ? except ?

Python, ?

Python 3?

+6
1

, . , , Python 3 try/except. :

as target, except.

except E as N:
   foo

except E as N:
    try:
        foo
    finally:
        del N

, , except. , , , .

, try/except, , exc as. , Python .

, as , :

>>> 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

, try/except , :

>>> 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
+6

All Articles