Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Thu Aug 1 18:22:17 EDT 2002


"Bengt Richter" <bokr at oz.net> wrote in message
news:aic0es$4v7$0 at 216.39.172.122...
> 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
> >thing
> >> 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
> >lambda?
> >> I'm always suspicious of phrasing like "all you need to do..." or
"the
> >only
> >> issue...", since it often indicates that insufficient thought has
gone
> >into
> >> a proposal. And the reason I'm suspicious when other people do it
is
> >because
> >> 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():
>          Button(
>             frame,
>             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.

I think you're on to something. I don't like extra syntax as
a rule, but in this case the pair of parenthesis does clarify the
matter.

John Roth

> >
>
> Regards,
> Bengt Richter





More information about the Python-list mailing list