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

Just van Rossum just at letterror.com
Fri Jul 7 12:09:26 CEST 2006


Evan Simpson wrote:

> 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).

Why couldn't at least augmented assignment be implicitly rebinding? It
has been suggested before (in the context of a rebinding operator), but
I'm wondering, is this also off the table?

    def counter(num):
        def inc():
            num += 1
            return num
        return inc

Reads very natural to me. It's likely the most frequent example of what
people try before they learn that rebinding to outer scopes isn't
allowed. It could Just Work.

Just


More information about the Python-Dev mailing list