question about scope

Jean-Paul Calderone exarkun at divmod.com
Thu Feb 16 12:40:22 EST 2006


On Thu, 16 Feb 2006 15:18:29 GMT, John Salerno <johnjsal at nospamgmail.com> wrote:
> [snip]
>
>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'? I thought that
>the only way to create a local namespace was if there *was* a function
>definition, so now I'm confused by the apparent difference that the
>authors are referring to. What's an example of a local scope without
>having a function definition? Loops and if statements, perhaps?

    print dir # comes from __builtins__
    dir = 10
    print dir # comes from globals
    def foo():
        dir = 10
        print dir # comes from locals
        def bar():
            print dir # comes from "enclosing scope" - foo's scope

Hope this helps,

Jean-Paul



More information about the Python-list mailing list