[Python-ideas] Allow lambda decorators

Aahz aahz at pythoncraft.com
Mon Feb 9 18:43:36 CET 2009

On Mon, Feb 09, 2009, Guido van Rossum wrote:
> In regard to the proposal of "bind i" syntax, I have a
> counter-proposal (as log as we're in free association mode :-):
> Define new 'for' syntax so that you can write
>   [lambda: i for new i in range(10)]
> or e.g.
>   fs = []
>   for new i in range(10):
>     def f():
>       return i
>     fs.append(f)
> The rule would be that "for new <name> in ..." defines a new "cell"
> each time around the loop, whose scope is limited to the for loop. So
> e.g. this wouldn't work:
>   for new i in range(10):
>     if i%7 == 6:
>       break
>   print i    # NameError
> I'm not saying I like this all that much, but it seems a more Pythonic
> solution than "bind i", and it moves the special syntax closer to the
> source of the problem.

Nice!  This does seem like it would work to quell the complaints about
for loop scoping while still maintaining current semantics for those who
rely on them.  Anyone wanna write a PEP for this?...

Anyone who does tackle this should make sure the PEP addresses this:

    for new x, y in foo:


    for new x, new y in foo:

(I'll channel Guido and say that the second form will be rejected; we
don't want to allow multiple loop targets to have different scope.  IOW,
``for new`` becomes its own construct, functionally speaking.)
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.

More information about the Python-ideas mailing list