Martin Panter added the comment: The first example seems to behave as I would expect. The UnboundLocalError is raised by the print() call, because the “x” variable has been deleted by the exception handler. Equivalent code without using “nonlocal”:
>>> def f(): ... x = None ... try: ... raise Exception() ... except Exception as x: ... pass ... print("x", x) # UnboundLocal due to exception handler ... >>> f() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 7, in f UnboundLocalError: local variable 'x' referenced before assignment In both cases, I think this is correct behaviour. See <https://docs.python.org/3.4/reference/compound_stmts.html#except>, which says “When an exception has been assigned using ‘as target’, it is cleared at the end of the except clause.” ---------- nosy: +vadmium resolution: -> not a bug _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue24321> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com