Codelog

foreach(Snippet aSnippet in CodeLog){ aSnippet.GetSolution(); }

Archive for the ‘lambda’ tag

if-else conditions in lambda functions

with 2 comments

On reading my last post singular form of a word, a friend of mine (Sathya) didn’t understand what I had done in the lambda functions. It was of the form lambda w: w[-3:] == ‘ies’ and w[:-3] + ‘y’. I had forgotten to explain about that in that post.

There is one small restriction in lambdas - you can’t use if-else constructs in them. If you want to use a condition check in your lambda, then it has to be of this form

lambda : <condition> and <if block> or <else block>

This is easy to understand when you compare it with conditional or ternary operators in C (?:) - condition?if block:else block.

Written by cnu

July 28th, 2008 at 3:53 pm

Posted in Python

Tagged with ,