[Python-ideas] Allow lambda decorators

Adam Olsen rhamph at gmail.com
Mon Feb 9 10:36:44 CET 2009

On Sun, Feb 8, 2009 at 11:02 PM, Chris Rebert <pyideas at rebertia.com> wrote:
> The basic idea was (using the keyword "bind" for the sake of argument):
> def func_maker():
>    fs = []
>    for i in range(10):
>        def f():
>            bind i #this declaration tells Python to grab the value of
> i as it is *right now* at definition-time
>            return i
>        fs.append(f)
>    return fs
> The `bind` declaration would effectively tell Python to apply an
> internal version of the `lambda x=x:` trick/hack/kludge automatically,
> thus solving the scoping problem.
> I now expect someone will point me to the thread I couldn't find
> and/or poke several gaping holes in this idea.

How about it's icky?  It feels like it's evaluating part of the inner
function when the function is defined.

There's a simple solution, which is to put it in the argument list:

    def f(bind i):
        return i

However, when compared to the existing def f(i=i): it just seems too
petty to be worth the increased language complexity.  That's where it
dies: too little benefit with too large a cost.

The only way I'd reconsider it is if default values were changed to
reevaluate with each call, but nobody should be proposing that again
until at least Python 4000, and only then if they come up with a much
better argument than we have today.  I doubt it'll ever happen.

Adam Olsen, aka Rhamphoryncus

More information about the Python-ideas mailing list