Message244357
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.” |
|
Date |
User |
Action |
Args |
2015-05-29 04:01:17 | martin.panter | set | recipients:
+ martin.panter, benjamin.peterson, yselivanov, whitequark |
2015-05-29 04:01:17 | martin.panter | set | messageid: <1432872077.59.0.126314985304.issue24321@psf.upfronthosting.co.za> |
2015-05-29 04:01:17 | martin.panter | link | issue24321 messages |
2015-05-29 04:01:16 | martin.panter | create | |
|