[Python-ideas] stealing "var" from javascript

Neil Toronto ntoronto at cs.byu.edu
Fri Apr 11 09:43:55 CEST 2008


Erick Tryzelaar wrote:
> On Thu, Apr 10, 2008 at 8:47 AM, Mathias Panzenböck
> <grosser.meister.morti at gmx.net> wrote:
>> It would also help with local functions!
>>
>>  def foo():
>>     var x = 1
>>
>>     def bar(y):
>>        x += y * 2
>>
>>     bar(55)
> 
> They've added something to py3k to handle this:
> 
> def foo():
>   x = 1
> 
>   def bar(y):
>     nonlocal x
>     x += y * 2
> 
>   bar(55)

Indeed. Funny enough, it was a similar plea for a variable declaration 
keyword (mine, in fact) that was the final straw. Thus, after a bit of 
bike shedding and collision checking on the keyword spelling, "nonlocal" 
was born...

Since the foremost use case has been handled, I expect a "var" proposal 
to go nowhere. The rationale, IIRC, is rather compression-oriented: in a 
language where only functions define inner scopes, it's far more likely 
that an assignment is meant to refer to the current scope than an outer 
scope. A declaration keyword is provided for the unlikely case, leaving 
the likely case declaration-free.

In general, the more compressed your code is, the harder it is to detect 
encoding errors. In this case, Python's designers decided on compression 
over easy error detection.

Neil



More information about the Python-ideas mailing list