[Python-3000] Draft PEP for outer scopes

Nick Coghlan ncoghlan at gmail.com
Mon Nov 6 23:31:11 CET 2006


Michael Urman wrote:
>>    def weird(local=True):
>>        if local:
>>            n = 1
>>        else:
>>            global n   # [ or nonlocal n, in Nick's ]
>>            n += 1
>>        return n
> 
> I'm not sure what you're both trying to explain here. First off, the
> above code yields a SyntaxWarning. Secondly all its accesses to n are
> global accesses. The global keyword is currently a modifier for the
> whole function. Thus the local parameter is misleading; it should be
> called reset.

We weren't really talking about global/nonlocal in general - I was merely 
pointing out the specific problem that permitting the shortcut form 
(declaration & assignment on the same line) increases the temptation to put 
the scope declaration somewhere other than at the start of the function.

As you point out, the compiler won't care (beyond possibly issuing a warning 
like the one you mention), but it can be misleading for human readers and writers.

Cheers,
Nick.


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


More information about the Python-3000 mailing list