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

Nick Coghlan ncoghlan at gmail.com
Tue May 2 13:32:15 CEST 2006

Phillip J. Eby wrote:
> 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.

I think symtable.c could wander back up the lexical block stack checking that 
target names are defined for augmented assignment statements.

That said, while I'd be happy to review a patch, I'm not going to try to write 
one :)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list