I know that in general python only makes new scopes for classes, functions etc., but I'm confused by the as
statement in a try/except block or context manager. Variables assigned inside the block are accessible outside it, which makes sense, but the variable bound with as
itself is not.
So this fails:
try:
raise RuntimeError()
except RuntimeError as error:
pass
print(repr(error))
but this succeeds:
try:
raise RuntimeError()
except RuntimeError as e:
error = e
print(repr(error))
What's going on with the variable bound with as
, and why don't normal python scoping rules apply? The PEP indicates that it's just a normally bound python variable, but that doesn't seem to be the case.