lambda functions

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Aug 31 04:16:39 EDT 2009


En Mon, 31 Aug 2009 04:41:57 -0300, Pierre <pierre.gaillard at gmail.com>  
escribió:

> I would like to know if it is possible to define a loop in a lambda
> function....
>
> How to manage the indents ? Example :
> s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
> [index]-1

You can't. lambda is just a way to define a short, inline, anonymous  
function, and its body can only contain expressions, not statements.

Even if what you want were legal, giving a name to the resulting  
expression kind of defeats the purpose of lambda - use a normal function  
instead:

def s_minus_1(s):
   "document usage here"
   for index,item in enumerate(s):
     s[index] -= 1

-- 
Gabriel Genellina




More information about the Python-list mailing list