question about what lamda does

Dan Bishop danb_83 at yahoo.com
Mon Jul 17 23:04:05 EDT 2006


nephish at xit.net wrote:
> Hey there,
> i have been learning python for the past few months, but i can seem to
> get what exactly a lamda is for.

It defines a function.

f = lambda x, y: expression

is equivalent to

def f(x, y):
   return expression

Note that lambda is an expression while def is a statement.

> What would i use a lamda for that i
> could not or would not use a def for ? Is there a notable difference ?
> I only ask because i see it in code samples on the internet and in
> books.

Lambdas are typically used as parameters to functions that take
functions as arguments, like property() and reduce().  You never *need*
to use one, but sometimes it's convenient.




More information about the Python-list mailing list