[Python-3000] The meaning of "global variable"

Ron Adam rrr at ronadam.com
Sun Nov 5 05:28:23 CET 2006


Greg Ewing wrote:
> Ron Adam wrote:
> 
>> How about limiting nonlocal to just the immediate parent scope and using 
>> 'parent' as the keyword?
> 
> That could lead to confusing situations. What should
> the following do:
> 
>    def f():
>      x = 42
>      def g():
>        def h():
>        parent x
>        x = 88
> 
> Should the assignment to x in h() create a name in
> the scope of g() even though there's no assignment
> in g() to establish that as its home scope? Should
> it be an error?
> 
> --
> Greg

Not confusing at all.  It would work just like global does in the same 
situation.  The parent statement here does not create a name, it only designates 
the scope where a name will be written.

I presume you meant:

    def f():
      x = 42
      def g():
        def h():
          parent x
          x = 88


The x would be a new local x in function g and have no effect on the x in 
function f.  Parent would do exactly what it means in this case, its a reference 
to a name in only the parent scope.

Unless you were to do:

    def f():
      x = 42
      def g():
        parent x
        def h():
          parent x
          x = 88

Then all references to x would be the local x in function f.

This is what I meant that it might be possibly to pull a reference down to inner 
scopes.  It gives more control on what name space you are writing to and less 
chance of writing to a grandparent scope unintentionally because of a variable 
name being left out, having its name changed, or has been deleted in the 
immediate parent function while editing.

I also don't think this would be too much trouble as nested functions don't tend 
to be more than 2 or 3 levels deep very often and if it becomes a problem of 
having too many parent statements, it's probably a good sign a class would be 
better.

IMHO, and if a suitable keyword meaning parent could be found.

   Ron



More information about the Python-3000 mailing list