[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Alex Gaynor alex.gaynor at gmail.com
Mon Sep 26 14:26:41 CEST 2011

Nick Coghlan <ncoghlan at ...> writes:
>     i = 88
>     def f():
>         nonlocal i from 17
>         print(i)
>         i += 1
>   def outer():
>     i = 17
>     def f():
>         nonlocal i
>         print(i)
>         i += 1
>     return f
> >>> f = outer()

You had me, you really did.  Right up until you showed the current equivalent.  
This strikes me as a few things.

Most importantly, as you noted yourself, a pretty rare case, even in C static 
variables are probably the rarest scope of variable.  This strikes me as a) not 
saving very much code, it's like crappy HFS instead of real sugar ;), and b)
not adding fundamental value, I think both blocks of code are equally readable.  
Other examples of syntatic sugar, such as decorators, have code motion
properties  that let you think about code in the places that makes sense, and I
don't think this has that.


More information about the Python-ideas mailing list