[Python-Dev] Lexical scoping in Python 3k

BJörn Lindqvist bjourne at gmail.com
Sat Jul 1 03:11:59 CEST 2006


> With "var":
>
>     var a = 3
>     def f():
>         var b = 4
>         def g():
>             var c = 5
>             a, b, c = 0, 1, 2   # changes outer a, outer b, and c
>         g()
>     f()
>
> Now i think this is a little bit weird, because the statement
> "var b = 4" in an outer scope changes the meaning of "b" in an
> inner scope.  But it does have the virtue of retaining behaviour
> compatible with today's Python, while offering a way to get proper
> lexical scopes for those who want to use them.
>
> Thoughts?  Other ideas?


Maybe an object, like self, for referring to enclosing scopes?

     a = 3
     def f():
         b = 4
         def g():
             c = 5
             outer.outer.a, outer.b, c = 0, 1, 2   # changes outer a,
outer b, and c
         g()
     f()

Chaining the keyword looks a little weird, but it is not often that
you have to refer to variables in the enclosing scope of the enclosing
scope. I have often wanted something similar to that for global
variables, instead of the global declaration:

cache = None
def init():
    if not global.cache:
        global.cache = init_cache()

-- 
mvh Björn


More information about the Python-Dev mailing list