python first assignment of a global variable

Miles Kaufmann milesck at umich.edu
Wed Jul 15 14:17:27 EDT 2009


On Jul 15, 2009, at 1:55 PM, Rodrigue wrote:

> Basically, I was very surprised to discover that e() raises an
> exception, but even more that e_raise() points to
> if not MY_GLOBAL Is the problem not really when I assign?
>
> My assumption is that some reordering is happening behind the scenes
> that creates a situation similar to the += which assigns hence expects
> to be at the local level.

The determination of whether a name is a reference to a local or  
global variable is made at compile time.  When a function contains a  
single assignment (or augmented assignment) to a name, the compiler  
generates bytecode such that all references to that name within the  
function will be looked up in the local scope only, including those  
before the assignment statement.

-Miles




More information about the Python-list mailing list