[Python-Dev] global variable modification in functions [Re: elimination of scope bleeding of iteration variables]

Guido van Rossum guido at python.org
Mon May 1 17:38:13 CEST 2006

On 5/1/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> While I agree that item #1 is a non-starter, it seems to me that in the
> case where the compiler statically knows a name is being bound in the
> module's globals, and there is a *non-argument* local variable being bound
> in a function body, the odds are quite high that the programmer forgot to
> use "global".  I could almost see issuing a warning, or having a way to
> enable such a warning.
> And for the case where the compiler can tell the variable is accessed
> before it's defined, there's definitely something wrong.  This code, for
> example, is definitely missing a "global" and the compiler could in
> principle tell:
>      foo = 1
>      def bar():
>          foo+=1
> So I see no problem (in principle, as opposed to implementation) with
> issuing a warning or even a compilation error for that code.  (And it's
> wrong even if the snippet I showed is in a nested function definition,
> although the error would be different.)
> If I recall correctly, the new compiler uses a control-flow graph that
> could possibly be used to determine whether there is a path on which a
> local could be read before it's stored.

Sure. This is a quality of implementation issue.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list