[issue26174] Exception alias cause destruction of existing variable

Zachary Ware report at bugs.python.org
Thu Jan 21 12:42:00 EST 2016


Zachary Ware added the comment:

I don't believe this is a bug.

1) try/except does not introduce a new scope
2) 'except E as N' is just name assignment
3) except clauses are documented to always delete the exception alias at the end of the except block[0].  The example in the docs could be clarified to show that

   except E as N:
       foo

is really more like

   except E:
       N = sys.exc_info()[1]
       try:
           foo
       finally:
           del N

Note that 'as' assignment with the 'with' statement also overrides a local variable of the same name.  The simplest way to avoid this issue is to use a different name for the exception alias.


[0] https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

----------
nosy: +zach.ware

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26174>
_______________________________________


More information about the Python-bugs-list mailing list