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.

classification
Title: incorrect: failed local variable referenced before assignment
Type: behavior Stage:
Components: None Versions: Python 3.0, Python 2.4, Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, paultjuhatwork
Priority: normal Keywords:

Created on 2009-09-03 11:25 by paultjuhatwork, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.py paultjuhatwork, 2009-09-03 11:25 3 test cases stripped down to the bare minimum
Messages (2)
msg92199 - (view) Author: Paul van der Linden (paultjuhatwork) Date: 2009-09-03 11:25
The attached python file will give the following output, which is 
incorrect behavior as far as I know:
"
this will fail
failed local variable 'in_std' referenced before assignment

this won't fail
Not failed
this won't fail either
Not failed
"

This is tested on windows with python2.6(standard msi) and on centos 
5.3 with python2.6 (custom rpm), python2.4 (system rpm), freebsd with 
python2.5 (system package), python2.6 ("hand" compiled) and python3.0 
("hand" compiled).

The attached code is stripped down to the bare minimum and therefore 
won't do anything usefull.
msg92201 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-09-03 11:44
This is not a bug.  The behaviour you're seeing is described here:

http://docs.python.org/reference/executionmodel.html#naming-and-binding

"If a name binding operation occurs anywhere within a code block, all
uses of the name within the block are treated as references to the
current block. This can lead to errors when a name is used within a
block before it is bound. This rule is subtle. Python lacks declarations
and allows name binding operations to occur anywhere within a code
block. The local variables of a code block can be determined by scanning
the entire text of the block for name binding operations."

In the failing example, the registerdecorator function contains an
assignment to in_std, so by the rules above in_std is local to the
function.  The 'if in_std' line therefore tries to lookup 'in_std' in
the local namespace;  it doesn't exist (yet), so an UnboundLocalError
exception occurs.
History
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51082
2009-09-03 11:44:35mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg92201

resolution: not a bug
2009-09-03 11:25:31paultjuhatworkcreate