[Python-ideas] Allow lambda decorators

Guido van Rossum guido at python.org
Mon Feb 9 19:12:43 CET 2009


On Mon, Feb 9, 2009 at 10:05 AM, spir <denis.spir at free.fr> wrote:
> Le Mon, 9 Feb 2009 09:09:18 -0800,
> Guido van Rossum <guido at python.org> a écrit :
>
>>   fs = []
>>   for new i in range(10):
>>     def f():
>>       return i
>>     fs.append(f)
>
> The difference I see between such an iteration-specific loop variable and a "declarative" version is that in the latter case it is possible to choose which name(s), among the ones that depend on the loop var, will actually get one "cell" per iteration -- or not. Hem, maybe it's not clear...
>
> For instance, using a declaration, it may be possible to write the following loop (I do not pretend 'local' to be a good lexical choice ;-):
>
> funcs = []
> for item in seq:
>        local prod      # this name only is iteration specific
>        prod = product(item)    # ==> one prod per item
>        def f():
>                return prod
>        funcs.append(f)
>        if test(item):
>                final = whatever(item)  # non-local name
>                break
> print "func results:\n%s\nend result:%s" \
>        %([f() for f in funcs],final)
>
> I do not like global & non-local declaration (do not fit well the overall python style, imo). So I would not like such a proposal, neither. But I like the idea to select names rather than a loop-level on/off switch.

You could use "new prod" for this. Or you could use "var prod" and
change the "for" proposal to "for var". It's a slippery slope though
-- how often do you really need this vs. how much more confusion does
it cause. You would need rules telling the scope of such a variable:
with "for new" that is easy, but what if "var foo" is not nested
inside a loop? Then it would essentially become a no-op. Or would it
introduce a scope out of the nearest indented block? This quickly
becomes too hairy to be attractive.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list