LAMBDA IS IT USELESS?

Bengt Richter bokr at accessone.com
Sat Jul 14 13:55:25 EDT 2001


On Sat, 14 Jul 2001 10:48:43 +0200, "Alex Martelli" <aleaxit at yahoo.com> wrote:

>"Bengt Richter" <bokr at accessone.com> wrote in message
>news:3b4f6eb5.1832748462 at wa.news.verio.net...
>> On Fri, 13 Jul 2001 18:01:37 +0200, "Alex Martelli" <aleaxit at yahoo.com>
>wrote:
>> [...]
>> >or to the def.  The ONLY difference is that lambda generates an
>> >anonymous function (and is an expression), while def generates a
>> >named function (and is a statement).
>>
>> In that case why not allow an anonymous version of def, e.g..,
>>
>>     map(
>>         def(x):
#>>             x**2
                return x**2 # thanks to Fredrik                    
>>         ,xrange(5))
>>
>> which would work like
>>
>>     map(
>>         lambda x:x**2
>>         ,xrange(5))
>
>Judging from this sole example, the only difference here would
>be the inconsequential syntax sugar of WRITING def(x) vs
>lambda x -- who cares for such trifles?
>
Sure. Sorry about the ambiguity (and unhelpful error in the example)

>Very different would be if the def was still a statement (and
>particularly a compound statement, including other statements):
Yes, that is what I was intending

>in that case, its anonimity would be no use UNLESS one fundamental
>principle of Python syntax was abrogated -- no expression can
>include statements.  If that were done, then there would be very
>wide-spread repercussions, needing a careful redesign of many
>apparently-unrelated things.
>
I didn't mean to abrogate the no-statements-in-expressions principle
in general, just within a fullfledged lambda definition.

Actually, it would have been clearer to leave def out of it, and say
I would like lambda to work just like def except returning the
function instead of binding a symbol to it.

In practice, since it's so easy to use def with a throwaway name,
the real question is when could the name mess things up. I don't
have an immediate answer ;-)

>From the grammar, we have
    funcdef: 'def' NAME parameters ':' suite
where
    parameters: '(' [varargslist] ')'
and
    lambdef: 'lambda' [varargslist] ':' test

So what would happen if that last "test" were
changed to "suite" ? If the end of "suite" is
well defined, why would there be a problem?

>
>> (OTOH you could make lambda multi-line ;-)
>
>lambda can be multi-line today.  I.e., you're free to break the
>line after the ':' in the above example.  Lines are not an issue.
>STATEMENTS vs EXPRESSIONS is the issue.
>
Sorry, I should have said "OTOH you could make lambda multi-statement" ;-)




More information about the Python-list mailing list