[Python-Dev] Re: closure semantics

Skip Montanaro skip at pobox.com
Thu Oct 23 17:46:33 EDT 2003


    John> How about (to abuse a keyword that's gone unmolested for too long)

    John>    global foo from def

    John> to declare that foo refers a variable in a lexically enclosing
    John> function definition?  This avoids to need to name a specific
    John> function (which IMHO is just a source of confusion over the
    John> semantics of strange cases) while still having some mnemonic value
    John> (foo "comes from" an enclosing function definition).

How do you indicate the particular scope to which foo will be bound (there
can be many lexically enclosing function definitions)?  Using my example
again:

    def outer(a):
        x = a
        def inner(a):
            x = 42
            def innermost(r):
                global x from def       # <--- your notation
                x = r
            print "  inner, x @ start:", x
            innermost(random.random())
            print "  inner, x @ end:", x
        print "outer, x @ start:", x
        inner(a)
        print "outer, x @ end:", x

how do you tell Python that x inside innermost is to be associated with the
x in inner or the x in outer?

Skip



More information about the Python-Dev mailing list