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.
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 "<stdin>", line 1, in <module> File "<stdin>", 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 -> closed resolution: not a bug components: - None stage: resolved |
| 2012年01月19日 16:08:34 | ezio.melotti | set | status: closed -> open nosy: + ezio.melotti messages: + msg151640 resolution: not a bug -> (no value) stage: resolved -> (no value) |
| 2012年01月19日 16:07:00 | michael.foord | set | status: open -> closed nosy: + michael.foord messages: + msg151639 resolution: not a bug stage: resolved |
| 2012年01月19日 16:05:30 | hippmr | set | status: closed -> open resolution: not a bug -> (no value) messages: + msg151638 |
| 2012年01月19日 16:03:50 | benjamin.peterson | set | status: open -> closed nosy: + benjamin.peterson messages: + msg151637 resolution: not a bug |
| 2012年01月19日 16:03:11 | hippmr | set | files:
+ over.py messages: + msg151636 |
| 2012年01月19日 16:02:19 | hippmr | create | |