a=[ lambda t: t**n for n in range(4) ]

Scott David Daniels Scott.Daniels at Acm.Org
Sat Apr 23 11:00:11 EDT 2005


Mage wrote:
> Scott David Daniels wrote:
>>
>>See, the body of your anonymous function just looks for "the current
>>value of n" when it is _invoked_, not when it is _defined_.
> 
> The "lambda functions" was an unclear part of the tutorial I read.
> Should I use them? Are they pythonic?
> As far I see they are good only for type less a bit.

As for most python features which are not "dead obvious", you should
only use them where they they make the code clearer than it would be
without the use of that feature.  In this case, I was responding to
a particular use of lambda which, in fact, had a flaw that made the
code do something different from what it looked like it was doing.
I felt the distinction was clearer in the lambda form than it would
have been with "def-style" function definitions.  One of the reasons
the distinction came out was that there was no confusing naming of
"what this function means," only program structure.  In a real program
that is a hindrance.  In the case of pointing out a flaw, it makes
differences between two versions of some code more obvious.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list