Message382967
This is just how local/nonlocal/global/builtin variables work in Python.
When you assign to a name anywhere inside of a function, all occurrences of that name refer by default to a local variable. So the line "ZeroDivisionError = 1" tells the foo() function that it has some local variable called "ZeroDivisionError". In order to make sure that the ZeroDivisionError always refers to the builtin exception, you need to add a global statement:
>>> def foo():
... global ZeroDivisionError
... try:
... 1/0
... except ZeroDivisionError as e:
... ZeroDivisionError = 1
>>> foo()
>>> ZeroDivisionError
1
See also: https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value |
|
Date |
User |
Action |
Args |
2020-12-14 09:23:10 | Dennis Sweeney | set | recipients:
+ Dennis Sweeney, xxm |
2020-12-14 09:23:10 | Dennis Sweeney | set | messageid: <1607937790.42.0.717826521331.issue42632@roundup.psfhosted.org> |
2020-12-14 09:23:10 | Dennis Sweeney | link | issue42632 messages |
2020-12-14 09:23:10 | Dennis Sweeney | create | |
|