[Python-3000] Draft PEP for outer scopes

Talin talin at acm.org
Thu Nov 2 19:00:56 CET 2006


Fredrik Lundh wrote:
> Talin wrote:
> 
>> One thing I don't understand in this discussion of re-purposing the 
>> 'global' keyword is how you handle the case of an inner function that 
>> *creates* a global.
>>
>> Right now, you can create a global variable from within a scope, even if 
>> that variable hasn't been declared yet:
>>
>>     def foo():
>>        global x
>>        x = 1
>>
>>     foo()
>>     print x
>>
>> However, if 'global' is simply a synonym for 'nonlocal', then how does 
>> it know *which* scope to create the variable in?
> 
> since what's a free variable and not is determined by static analysis, 
> and free variables are "owned" by the innermost scope they're used in, 
> I'm not sure why you even asking that question.

Right now, 'global' allows you to create a global variable from within a 
function, even if that global does not yet exist:

 >>> def foo():
...    global x
...    x = 1
...
 >>> foo()
 >>> print x
1

If you change the behavior of 'global' to be the same as 'nonlocal' as 
has been proposed, then this effectively becomes impossible - you can no 
longer set any global that hasn't already been pre-declared.

-- Talin


More information about the Python-3000 mailing list