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?