Let's Talk About Lambda Functions!

Bengt Richter bokr at oz.net
Thu Aug 1 20:59:40 CEST 2002

On Mon, 29 Jul 2002 14:43:11 -0400, "John Roth" <johnroth at ameritech.net> wrote:

>"Steve Holden" <sholden at holdenweb.com> wrote in message
>news:7Rc19.92310$724.23106 at atlpnn01.usenetserver.com...
>> "John Roth" <johnroth at ameritech.net> wrote ...
>> > "Fredrik Lundh" <fredrik at pythonware.com> wrote...
>> >
>> > What's the problem with that? If we give up the notion
>> > that anonamous functions need to look like expressions,
>> > it should be simple. Just stick the function definition inline,
>> > indentation and all. The only issue is defining a new keyword,
>> > since lambda doesn't work that way. "def" would probably
>> > work, since it's only allowed at statement level now.
>> >
>> I'd like to see an example of this, since it appears you haven't fully
>> thought this through. You mean you'd like to be able to write some
>> like the following:
>>     for word in 'fee fie fo fum'.split():
>>         Button(frame, command=lambda:
>>             print word)
>> This is currently acceptable in Python because the lambda's inside a
>> parenthesised argument list. How would I add a second line to the
>> I'm always suspicious of phrasing like "all you need to do..." or "the
>> issue...", since it often indicates that insufficient thought has gone
>> a proposal. And the reason I'm suspicious when other people do it is
>> I've watched me doing it enough to get into trouble!
>> Conclusion: this idea is currently 0.345-baked :-)
>    for word in 'fee fie fo fum'.split():
>        Button(frame, command = def ():
>                print word
>            )
IMO enclosing the anonymous def expression in parens improves clarity:

     for word in 'fee fie fo fum'.split():
         Button(frame, command = (
            def ():
                print word

Or, expanded more:

     for word in 'fee fie fo fum'.split():
            command = (
                def ():
                    print word

I like your idea (I thought of it too ;-) but I think putting the
anonymous def expression inside parens of its own makes it clearer.
Maybe they should even be required, to encourage/enforce a clear style
(and probably make parsing easier).

The indentation of the 'def' serves as the reference indentation for
determining the end of the suite block as usual, but this 'def' indentation
can be anywhere for source beautification purposes, since it's inside parens.

>This isn't really the world's best example, because IMO, the
>lambda form is easier to read for something this small. However,
>it does give the guts of the idea:
>1. instead of lambda (which needs to stay as is for backward
>compatibility) it uses 'def' as the keyword.
>2. The remainder of the syntax exactly models def, including
>statement indentation within the expression. Notice that the
>first statement in the anonamous function has to be indented
>two from the preceeding line, because the continuation of
>the expression has to still be indented from the line with the
>'def', and dedented from the statements.
IMO this is a reason to use parens around the whole def, thus
decoupling its indentations from its outside context.

>3. The use of 'def' here reflects a comment from one of
>the posters that it's easy for people without experience in
>languages with first class functions to miss the fact that Python
>has them.
>Hope this helps. I considered it simple enough that it really
>didn't need an example.

Bengt Richter

More information about the Python-list mailing list