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 tim.peters
Recipients
Date 2001-09-21.20:15:03
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Logged In: YES 
user_id=31435

Whether a given name is local or global in a given scope is 
a property determined at compile-time, not at runtime.  See 
the Reference Manual for details.  In brief, if a name is 
bound ("assigned to") *anywhere* in the body of a function, 
that name is local throughout the entire function.  You 
assign to y in the body of g(), therefore y is a local 
variable everywhere in g(), and you reference y's value in g
() before giving it a value (hence it's an unbound local at 
the time you first reference it, hence UnboundLocalError in 
recent Pythons).  The y in the body of g() has nothing to 
do with the module-level vrbl y.  If you intended to 
reference the module-level vrbl y, then you need to put

global y

as the first statement in g().

But it's impossible for me to guess what you intended -- 
and it's also impossible for Python.

If you're familiar with C, it's exactly the same as

int y = 10;

int g() {int y; if (y) y = 100; return y;}

except that in C you'd get random crap out of that function 
(due to referencing an uninitialized auto) instead of a 
reliable exception.  The difference is that Python doesn't 
have explict declarations for local variables, but the fact 
that you assigned to y within the body of g makes y a local 
just as much as y is function-local in the C example above.
History
Date User Action Args
2007-08-23 13:56:30adminlinkissue463640 messages
2007-08-23 13:56:30admincreate