This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dennis Sweeney
Recipients Dennis Sweeney, xxm
Date 2020-12-14.09:23:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1607937790.42.0.717826521331.issue42632@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-12-14 09:23:10Dennis Sweeneysetrecipients: + Dennis Sweeney, xxm
2020-12-14 09:23:10Dennis Sweeneysetmessageid: <1607937790.42.0.717826521331.issue42632@roundup.psfhosted.org>
2020-12-14 09:23:10Dennis Sweeneylinkissue42632 messages
2020-12-14 09:23:10Dennis Sweeneycreate