Is this a good use for lambda

Jeff Shannon jeff at
Sat Dec 18 02:01:09 CET 2004

Charlie Taylor wrote:

>root = findRoot(xBeg, xEnd, 
>       lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)

Um, so which parts of this are the actual lambda??  Just from reading 
that, it's hard to be sure.  My mind keeps wanting to break at 'lambda 
x: y2 + lp*(x-x2)', but when I stop to think about it, I know that it 
must be the entire segment between commas ('lambda x: y2 + lp*(x-x2) 
-wallFunc( x )[0]'). 

This is exactly why I don't like using lambdas.  Very easy to get 
confused by the syntax, and (IMO) not much benefit. 

>I have tried using named functions instead of using lambda functions,
>however, I always end up with a convoluted, hard to follow mess.

See, to my mind, the above is a bit convoluted and hard to follow.  I'd 
prefer to see something like:

    def func(x):
        answer = y2 + (lp * (x-x2)) - wallFunc(x)[0]
        return answer

    root = findRoot(xBeg, xEnd, func, tolerance=1.0E-15)

(I'm hoping, of course, that y2, x2, and lp are local variables, rather 
than global variables...)

I find this named function to be much more clear in regards to what's 
part of the lambda and what's actually a parameter to findRoot().  I 
suppose that opinions may vary, however.

Jeff Shannon
Credit International

More information about the Python-list mailing list