Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Sat Jul 27 08:20:20 EDT 2002


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:Lap09.24994$724.6190 at atlpnn01.usenetserver.com...
> "John Roth" <johnroth at ameritech.net> wrote in message
> news:uk3abpr4treeb9 at news.supernews.com...
> >
> > "Britt A. Green" <python at experimentzero.org> wrote in message
> > news:mailman.1027705851.8443.python-list at python.org...
> > > So I know what lambda functions are, their syntax and how they're
> > used.
> > > However I'm not sure *why* one would use a lambda function. What's
the
> > > advantage that they offer over a regular function?
> >
> > They let you put short, one-time functions inline. In some
> > environments (Tkinter comes immediately to mind) there
> > are lots of places where short, one-line functions are useful.
> > Putting a short, one-time function inline aids readability,
> > (at least for people who regard lambdas as readable.)
> >
> > IMO, if the function is of any significant length, or if it
> > duplicates functionality from elsewhere, it should be
> > named and written separately.
> >
>
> And let us not forget that those who hate naming one-off functions can
reuse
> the names if they want.
>
> def a(x):
>     print x
> def a(y):
>     print y*y
>
> is perfectly legal Python.

It took me a moment to realize what you were getting at. Granted,
it's perfectly legal Python, but the first def is rebound by the second,
so it has absolutely no effect unless (and this is what wasn't obvious
from the example) it's bound to something else before the second
definition.

Also, that's probably going to confuse lots of software. I'd suspect
that most class browsers, if they found six methods with the same
name in a class, would report either the first or the last, or else
flag it as an error.

John Roth






More information about the Python-list mailing list