Proposal: unshadow operator '&'

Kay Schluehr kay.schluehr at gmx.net
Sat Aug 6 13:07:27 EDT 2005


seberino at spawar.navy.mil wrote:
> I've heard 2 people complain that word 'global' is confusing.
>
> Perhaps 'modulescope' or 'module' would be better?
>
> Am I the first peope to have thought of this and suggested it?
>
> Is this a candidate for Python 3000 yet?
>
> Chris

Maybe a solution would be best that is able to resolve shadowing of a
variable in an explicit manner.

Example. you might have the following nested function:

x = 0
def h():
    x = 1             # module scoped x will be shadowed
    def g():
        x = 2         # module scoped x and h's local x will be
shadowed
        def f():
            print x   # printing x defined locally in g
        f()
    g()

The module level x is always accessible from each inner function using
the global keyword but it is not possible to access x defined locally
in h from f.

Two hypothetical variants using an "unshadow operator" & explicitely:

x = 0
def h():
    x = 1
    def g():
        x = 2
        def f():
            print &x     # unshadowing x defined in h
        f()
    g()


x = 0
def h():
    x = 1
    def g():
        x = 2
        def f():
            print &&x     # unshadowing x defined on module level
        f()
    g()

Since we can always shadow some module scoped variable defining a local
one we might create a side-effect binding values to the unshadowed
name:

x = 0
def f():
    x = 1
    &x = 7

>>> f()
>>> x
7

Regards,
Kay




More information about the Python-list mailing list