[Python-3000] Sky pie: a "var" keyword

Nick Coghlan ncoghlan at gmail.com
Tue Oct 10 16:45:05 CEST 2006


Ka-Ping Yee wrote:
> On Tue, 10 Oct 2006, Fredrik Lundh wrote:
>> Nick Coghlan wrote:
>>> Any proposal such as this also needs to addresses all of the *other* name
>>> binding statements in Python:
>>>
>>>    try/except
>>>    for loop
>>>    with statement
>>>    def statement
>>>    class statement
>> +    import statement
>> +    list comprehension (in 2.X)
> 
> The only sane proposal i've seen that consistently addresses all
> these cases, doesn't create new ambiguous situations, and doesn't
> break most existing Python code is the "nonlocal" declaration.
> 
>     nonlocal x
> 
> means
> 
>     "Don't make a new binding for x in the local scope."
> 
> With this declaration, any binding statements modify the x that
> is visible from an outer scope instead of creating a local x.

Agreed - I believe the only real problem with the idea was that nobody could 
come up with a name for this statement that was clearly the right name.

Re-using 'global' wasn't popular because it would actually be *wrong* for the 
new semantics (the name might not be right), and other contenders like 'outer' 
and 'nonlocal' had their share of critics, too ('outer' isn't quite right 
because the name used is rebound in the innermost lexical scope that defines 
it, while 'nonlocal' had the problem of being a negative definition). Jeremy 
Hylton provided a nice list of the goals for a good choice of keyword [1] 
('global' failed criteria #2, whereas 'nonlocal' passed criteria #2 but failed 
the rest).

As mentioned in that thread, something like 'external' would be a fairly nice 
fit, but the terminology baggage from C/C++ could be a problem (since we would 
mean external to the current scope, but C/C++ uses the term to mean external 
to the module. That said, anyone trying to interpret Python scoping in terms 
of C/C++ scoping is going to get horribly confused in fairly short order, 
particularly if lexically scoped variables are involved).

     def f():
         x = 0
         def g(i=1):
             external x
             x += i
             return x
         return g

Cheers,
Nick.

[1] http://mail.python.org/pipermail/python-dev/2006-July/067205.html

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


More information about the Python-3000 mailing list