python first assignment of a global variable

Rodrigue rodriguealcazar at gmail.com
Wed Jul 15 16:27:11 EDT 2009


> MY_GLOBAL, by virtue of being assigned to later in the function, and the
> absence of a global statement, is identified as a local variable.

> 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

Alright. I didn't know that. I thought the entire scope (local, then
global) was considered in every situation. It does explain the
observed behaviour then.

I'm surprised I never bumped into that before, but I'm glad I learnt
something new about python today.

Thanks Emile and Miles for the explanation!



More information about the Python-list mailing list