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

Ron Adam rrr at ronadam.com
Fri Nov 3 20:55:50 CET 2006


Ka-Ping Yee wrote:
> On Fri, 3 Nov 2006, Greg Ewing wrote:
>>> Before it is reasonable to change the meaning of "global", we would
>>> need to have coherent answers to these questions:
>>>
>>>     1.  What is the global namespace?
>> Under the proposal, there is no such thing as
>> "the" global namespace, so the question is
>> meaningless.
>>
>> There's no such thing now, either -- there
>> are just (multiple) module-level namespaces.
> 
> Okay... tell that to all the writers of Python books and webpages
> that mention "the global namespace".
> 
> Should we remove any occurrences of this phrase from the official
> Python documentation as well?
> 
>>> either you have
>>> to say (#1) there is no longer such a thing as "the global namespace",
>> As I said, there isn't one now. Which namespace
>> is "the global namespace" depends on which module
>> you're looking from. Under the proposal, it would
>> depend on which scope you're looking from.
> 
> No -- it's much worse than that.  That's the problem: even if you
> know which scope you're looking from, you wouldn't be able to point
> to anything and say "that's the global namespace".  If we repurpose
> "global" to mean "nonlocal", the declaration "global a, b, c" might
> mean that a, b, and c all belong to different namespaces.
> 
> Right now, in any given context, "the global namespace" has a clear
> meaning: the namespace shared by the whole file.  With the new meaning
> of the "global" keyword, "global namespace" becomes meaningless no
> matter what the context.  In Python, the concepts of "global", "global
> variable", and "global namespace" are too well established to destroy.
> 
> 
> -- ?!ng

I'll put in a 'Me Too' here.  (For what it's worth.)  You've convinced me that 
reusing global in verbatim for both global and nested parent scopes would create 
a confusing difference between 2.x and 3.x versions at least in documentation.


How about limiting nonlocal to just the immediate parent scope and using 
'parent' as the keyword?

Are there many common use cases for accessing grandparent name spaces?  That 
might still be done by repeating 'parent' declarations in nested functions for a 
name and thereby pulling access to it down the tree.  That would be vary 
explicit and since it's not something you would do very often, it wouldn't be 
much of a bother.

Limiting it to only the parent scope may also limit possible unintended side 
effects in the case a name is not in the scope that was expected.  (Possibly due 
to omission or deleting it at some point while editing.)

Cheers,
    Ron







More information about the Python-3000 mailing list