[Python-Dev] Why does "except Ex as x" not restore the previous value of x?

2020年11月17日 02:03:08 -0800

Hi,
I'm wondering why
```
x = "value"
try:
 1/0
except Exception as x:
 pass
```
does not restore "value" to x after
the `except` block.
There doesn't seem to be an explanation for this behavior in the docs or PEPs, that I can find.
Nor does there seem to be any good technical reason for doing it this way.
Anyone know why, or is just one of those unintended consequences like `True == 1`? Here's an example of restoring the value of the variable after the `except` block:
>>> def f(x):
... try:
... 1/0
... except Exception as x:
... pass
... return x
...
>>> f("hi")
'hi'
Cheers,
Mark.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/KGYRLITEPB22ZZO4N7DD4A7QP7FQS6JO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to