[Python-Dev] Explicit Lexical Scoping (pre-PEP?)

Evan Simpson evan at 4-am.com
Fri Jul 7 01:08:22 CEST 2006


Talin wrote:
> I propose to create a new type of scoping rule, which I will call 
> "explicit" lexical scoping, that will co-exist with the current 
> "implicit" scoping rule that exists in Python today.

I'd like to toss one more variant into the mix.  If we really need to
address variables in an intermediate scope, the most explicit way that I
can think of doing so is to write (using Philip's example):

def counter(num):
    scope as outer # "outer" is an arbitrary identifier
    def inc():
        outer.num += 1
        return outer.num
    return inc

This is somewhat similar to the unworkable "use the function name"
method that's been suggested.  The "scope as X" statement associates the
name "X" with the namespace of local variables in the scope in which it
is executed.  Such names are "lexically scoped", and only allow access
to or rebinding of existing names from the originating scope (i.e. no
"del outer.num" allowed).

Cheers,

Evan @ 4-am



More information about the Python-Dev mailing list