question about scope

John Salerno johnjsal at NOSPAMgmail.com
Thu Feb 16 11:43:58 EST 2006


Fredrik Lundh wrote:
> John Salerno wrote:
> 
>> But my real question is this, which is related to the above:
>>
>> "Name references search at most four scopes: local, then enclosing
>> functions (if any), then global, then built-in."
>>
>> I understand what global and built-in are, and I thought I understood
>> the concept of local too, but when I got to this sentence (and the
>> previous sentence), I became confused about the first two scopes. What's
>> the difference between 'local' and 'enclosing functions'?
> 
> consider a nested function:
> 
>     var1 = "global"
> 
>     def outer():
> 
>         var2 = "enclosing"
> 
>         def inner():
> 
>             var3 = "local"
> 
>             print var1, var2, var3
> 
>         inner() # call it
> 
> inside "inner", var1 refers to the global variable, var2 to the enclosing
> variable (which is local to "outer"), and var3 to the local variable.
> 
> "enclosing scope locals" are also called "free variables".
> 
> </F>
> 
> 
> 

Thanks guys. It seems like nested functions were what the authors had in 
mind, so that makes a lot more sense now.

But as far as ifs and loops, is there such a thing as scope in them? For 
example, if I assign a variable within an if statement, is it usable 
anywhere else?



More information about the Python-list mailing list