question about scope

Fredrik Lundh fredrik at pythonware.com
Thu Feb 16 10:56:24 EST 2006


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>






More information about the Python-list mailing list