Issue 13827: Unexecuted import changes namespace (original) (raw)

Created on 2012-01-19 16:02 by hippmr, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
main.py hippmr,2012-01-19 16:02 file that demonstrates the bug, needs follow-on over.py also
over.py hippmr,2012-01-19 16:03 additional file need to run main.py
Messages (6)
msg151635 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:02
A local *unexecuted* import appears to be changing the namespace. Attached files are ready to run. # over.py SOMETHING = "overridden" # main.py OVERRIDE = False SOMETHING = "original" def main(): #global SOMETHING # uncomment and it works if OVERRIDE: from over import SOMETHING # comment out and it works pass print SOMETHING # UnboundLocalError: local variable 'SOMETHING' referenced before assignment The SOMETHING variable has a value from the module global namespace, but it gets lost due to an import that is never executed. I would think an unexecuted statement shouldn't have any effect on anything. The second file will have to be submitted in a follow-on, it appears
msg151636 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:03
Add'l over.py file
msg151637 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-01-19 16:03
Not a bug. Basically, import is an explicit assignment statement.
msg151638 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:05
Even an *unexecuted* import assignment statement?
msg151639 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-01-19 16:07
hippmr: the problem is that by importing SOMETHING inside that function you're creating a *local variable* called SOMETHING. If the override isn't executed, and SOMETHING isn't global, then that local variable doesn't exist - which is why you get that error. So even if the import isn't executed, its existence in the function tells Python that name is local to the function.
msg151640 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-01-19 16:08
>>> OVERRIDE = False >>> SOMETHING = "original" >>> >>> def main(): ... if OVERRIDE: ... SOMETHING = None ... print SOMETHING ... >>> main() Traceback (most recent call last): File "", line 1, in File "", line 4, in main UnboundLocalError: local variable 'SOMETHING' referenced before assignment http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
History
Date User Action Args
2022-04-11 14:57:25 admin set github: 58035
2012-01-19 16:09:00 ezio.melotti set status: open -> closedresolution: not a bugcomponents: - Nonestage: resolved
2012-01-19 16:08:34 ezio.melotti set status: closed -> opennosy: + ezio.melottimessages: + resolution: not a bug -> (no value)stage: resolved -> (no value)
2012-01-19 16:07:00 michael.foord set status: open -> closednosy: + michael.foordmessages: + resolution: not a bugstage: resolved
2012-01-19 16:05:30 hippmr set status: closed -> openresolution: not a bug -> (no value)messages: +
2012-01-19 16:03:50 benjamin.peterson set status: open -> closednosy: + benjamin.petersonmessages: + resolution: not a bug
2012-01-19 16:03:11 hippmr set files: + over.pymessages: +
2012-01-19 16:02:19 hippmr create