problem with variable scoping
Fredrik Lundh
fredrik at pythonware.com
Thu May 27 04:43:20 EDT 1999
(I might be busy, but don't expect me to let you guys get
away with things like this while I'm off-line ;-)
> Ummmm.....Tim, I beg to differ: it is more like the C++
>
> void process(const char *line)
> {
> if (last_line_empty)
> ;
> else
> printf("<P>\n");
> int last_line_empty;
>
> last_line_empty = 1;
> }
>
> The Python code shadows last_line_empty only *after* the "decleration"
if it worked this way, do you really think Michael would have
gotten a NameError in the first place?
here's the full story:
http://www.python.org/doc/current/ref/execframes.html says:
Whether a name is local or global in a code block is determined
by static inspection of the source text for the code block: in the
absence of global statements, a name that is bound anywhere in
the code block is local in the entire code block; all other names
are considered global. The global statement forces global inter-
pretation of selected names throughout the code block.
...
Local names are searched only on the local namespace.
..
When a name is not found at all, a NameError exception is
raised.
note the use of "anywhere", "throughout", and "only".
</F>
More information about the Python-list
mailing list