[Python-3000] Draft PEP for outer scopes

Nick Coghlan ncoghlan at gmail.com
Sat Nov 4 04:36:52 CET 2006


Neil Toronto wrote:
>>> The point is to make the statement easy to explain.
>>>     
>> It's even easier to explain if assignment isn't
>> allowed at all.
>>   
> 
> Sorry about the "mee to" post, but me too: +1 on no assignment allowed. 
> I can't think of any way to do it that would be intuitive and obvious to 
> *most* people and wouldn't require a separate FAQ entry besides the one 
> that explains 'global' and 'nonlocal.'

'Me too' posts can be useful sometimes :)

Count me as another +1 on disallowing the shorthand version.

Rationale:
   1. We've lived without it in the case of global
   2. We can add it later if we decide we really need it, but if we start with 
it and it turns out to be confusing or error-prone, taking it away would be a 
serious pain.
   3. Leaving it out ducks some hard questions like "what does this do?":

   def weird(local=True):
       if local:
          n = 1    # Does this alter the closure variable?
       else:
          nonlocal n += i
       return n

Cheers,
Nick.

P.S. For those who asked "what's the point of the shorthand?", it's intended 
more for use with augmented assignment rather than basic assignment:

   def inc(i=1):
       nonlocal n += i
       return n

I can see the benefit in shortening toy examples, but I don't think it even 
comes close to being worth the extra complexity.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list